Setting the selected item in a Spinner widget - java

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

Related

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.

NavigationDrawer update items

I'm using the navigation drawer example in Android. What's the easiest way to change the string items in the adapter dynamically? Do I just create a new adapter and set it with my new values?
The problem is I'm altering the string items based on user state logged in and logged out. How do I access it from a non static context and just say update list adapter? It doesn't seem to be re-drawing itself and running my adapter code which is dynamic based on user state, so I guess it runs/inits once and I have to create and load a new adapter if I want to change it later?
Thanks.
You haven't given us much to go on (a little code would be nice), but you can update the data in the ListView by calling notifyDatasetChanged() on the Adapter. If you are not using a custom Adapter e.g. an ArrayAdapter then you can just create a new Adapter instance with different String values and set it to the ListView.

Set Spinner selection doesn't work

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.

Spinner Contents in Android

When you work with spinners in android, do you have to use a spinner adapter if you set the array in a spinner to a string array declared in strings.xml?
You can use any kind of adapter. For example I use a CursorAdapter to populate from sqlite.

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