I want to have three Spinners with contents which depend on each other.
E.g. Spinner1 displays {item1, item2} and Spinner2 either {item3, item4} or {item5, item6} depending on whether item1 or item2 is selected on Spinner1.
The same I want for Spinner 3, which reacts to changes of Spinner1 and/or Spinner2.
For the latter, I have to determine first which of the possible value sets is shown atm in Spinner2.
My question is kind of similar to this question, but I don’t know what to do after getting the adapter.
That’s what I have so far:
ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
//...
}
I get the adapter, ask for the first value and compare it to the first String value of my Array to identify it. It doesn’t at all seem elegant to me. Is there an easier solution?
You say the contents of the later spinners depend on the selection in the earlier ones, but the code you posted depends only on the contents of a spinner.
adapter1.getItem(0)returns the first item in the list, not the currently selected item. To get the currently selected item, use the spinner’s (not the adapter’s)getSelectedItem()method.You could, for example, put something like this in your first Spinner’s onItemSelectedListener (edited based on your comment below):
Then place something similar in the second Spinner’s onItemSelectedListener. Get the selected items from the first and second Spinners using
getSelectedItem()(or the item positions usinggetSelectedItemId()for the first and the position parameter for the second). Use the selected items to set up the third.Edit: The OnItemSelectedListener for the second Spinner would look something like this.