Set Spinner selection doesn't work - java

I'm trying to set a spinner with setSelection in order to display a specific option as follows:
Spinner gender;
gender = (Spinner) findViewById(R.id.spinner_edit_gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.gender_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(adapter);
if (strGender.equals("male"))
gender.setSelection(0,true);
else
gender.setSelection(1,true);
It always sets the first option of the spinner instead of what i want
Anyone know why and can help me plz?
Thanx

Try using setSelection(int) instead of setSelection(int, boolean).

Two things to try:
1.) I'm going to take a shot in the dark here and suggest that you try defining the options to your spinner in it's XML. This is what I'm in one of my apps where I use setSelection() successfully.
<Spinner android:id="#+id/spinner_edit_gender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/genders"
android:entries="#array/gender_array"/>
and then your code becomes:
Spinner gender = (Spinner) findViewById(R.id.spinner_edit_gender);
if (strGender.equalsIgnoreCase("male"))
gender.setSelection(0,true);
else
gender.setSelection(1,true);
or if we want to get a little fancier:
Spinner gender = (Spinner) findViewById(R.id.spinner_edit_gender);
(strGender.equalsIgnoreCase("male")) ? gender.setSelection(0,true) : gender.setSelection(1,true);
I changed equals() to equalsIgnoreCase() because you the values in your array don't match the case of the values in your code in your example ("male" vs "Male").
2.) When I was typing the first part I noticed the type of your ArrayAdapter is CharSequence. Try changing that to String and then test your original code. However, depending on your needs, you might want to consider going with defining the options in XML because, at least in my opinion, it's a cleaner and simpler way of defining options for a spinner than dealing with ArrayAdapters.

Related

Spinner with hint above selected element's string

I want to realize a Spinner with a "TextView with a hint", I already tried to pass another view beside R.layout.simple_spinner_item to the ArrayAdapter, but it doesn't work.
I want to do this by editing the Spinner element, not using a layout containing the TextView and the Spinner, how could i do it? I don't know how to realize a CustomAdapter, but, could that be the right way?
EDIT: Uploaded image of the Spinner Example (I hope it isn't broken)

Spinner inandroid studio

I have 2 spinners and 1 edit text. How to get selected spinner item. I need to know witch ones are selected, so I could show conversion resoult in other activity.
First spinner have 2 items ( Celsius and Fahrenheit) second also. How to call one of 2 methods depending on spinner item select.
Create a reference for your spinners. Extract the selected value of the spinners like this:
String spinnerValue = String.valueOf(spinnerReference.getSelectedItem());
Considering that spinnerValue can contain either "Farenheit" or "Celsisus", you can use a switch block to call the appropriate method.
If you want to check currently selected item in the spinner you should have an instance of it. If you don't make sure you assign an id (for example "unitSpinner") to it in the layout file and then in the Activity's onCreate method call Spinner spinner = (Spinner) findViewById(R.id.<idYouAssigned>). Then you can call spinner.getSelectedItem() which will return either "Celsuius" or "Fahrenheit" depending on which one the user selected.

android get an integer from a spinner similar to that of the method used for strings

In two parts of my app I have spinners. One spinner provides a set of strings, and the one I'm currently working on is supposed to allow the user to select a preset number (10, 25, 50, 100) of questions to take a practice test with that many questions.
I figured it'd be a simple copy/paste of the current spinner that works, while just changing the toString(); to toInteger(); but it doesn't seem to be the case. As I looked up how to retrieve an integer from a spinner, it looks significantly different than how I implemented my spinner of Strings. I know there's more than one way to do most things, but I'm curious if there's a direct translation from strings to integers when using spinners?
Basically, how do I use the same setup to achieve a spinner for integers?
Java Method:
//Get spinner selection
final Spinner userInputSpinner = (Spinner) findViewById(R.id.feedbackSpinner);
String TYPE = userInputSpinner.getSelectedItem().toString();
XML Spinner:
<Spinner
android:id="#+id/feedbackSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="#string/feedbackType"
android:entries="#array/feedbackChoice"/>
Arrays XML:
<!--ARRAY FOR FEEDBACK SPINNER-->
<string-array name="feedbackChoice">
<item>#string/feedbackCompliment</item>
<item>#string/feedbackSuggestion</item>
<item>#string/feedbackWrong</item>
<item>#string/feedbackIssue</item>
</string-array>
If I understand your problem, I think you need a conversion from string to int.
Try that :
Java Method:
//Get spinner selection
final Spinner userInputSpinner = (Spinner) findViewById(R.id.spinnerInt);
int TYPE = Integer.parseInt(userInputSpinner.getSelectedItem().toString());
XML Spinner:
<Spinner
android:id="#+id/spinnerInt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/intChoice"/>
Arrays XML:
<string-array name="intChoice">
<item>10</item>
<item>25</item>
<item>50</item>
<item>100</item>
</string-array>

Setting the selected item in a Spinner widget

In my Android app, I have an Activity with a Spinner widget. I created it with the following code in onCreate():
this.playerPositionSpinner = (Spinner) this.findViewById(R.id.player_position_text);
ArrayAdapter<CharSequence> positionsAdapter = ArrayAdapter.createFromResource(this, R.array.positions, android.R.layout.simple_spinner_item);
positionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.playerPositionSpinner.setAdapter(positionsAdapter);
Later I want to set the selected item based on a String which I know is a valid item in the Spinner's list? All of the Spinner.setSelection() methods take an int parameter for the position. How do I obtain the position of a String in the Spinners drop-down list?
(You have posted some code now! Yeah! This simplifies my example drastically.)
I recommend using Spinner#setSelection() after calling setAdapter():
playerPositionSpinner.setSelection(positionsAdapter.getPosition("Watermelon"));
Obviously you can replace "Watermelon" with something relevant for your app.
Use your spinner setSelection method:
http://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int, boolean)
You need to pass it the integer value of the location of the item. to get the value use your adapter.
You'll need to call playerPositionSpinner.setSelection(position) after creating and setting the Spinner's ArrayAdapter

Can i add components like spinner etc. in my listview with my listitems?

I am using a listview with n number of listitems.
I have need to add a spinner with all my listitems.
Is there any option to add the spinner dynamically.
Because i can't determine the number of listitems.
Yes you can, for this you need to create a custom list adapters. See this tutorial for more information
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Customize row.xml to add Spinner there as per your layout.

Categories