1

I'm stuck on a problem with spinners.

Actually, I have some tabs with spinners and EditText that I create without any porblems. I have to keep the datas that the user type in an xml file. So I create an xml file and it workd fine.

I also have a loading tab that permis to load datas saved in this xml. So I load the file, I parse it and I fill my EditTexts without any problems.

The problem is from the spinner : I can't put the data from the XML I created in my spinner.

I tried by saving it as a string and then I tried to load it in the spinner by this way :

(Spinner) spinner.setPrompt(string);

That doesn't work, I have the default value but not the saved value. I also tried to save the integer of the choice made by the user. And then to reload it by this way :

(Spinner) spinner.setSelection(Integer.parseInt(string));

I don't think I use it properly cause I have a FC.

So I don't really know how to proceed to load the value from the xml in my spinner.

Any idea ?

Thanks !

beluga
  • 193
  • 3
  • 17

1 Answers1

4

Try this:

String myString = "some value"; //the value you want the position for

ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter

int spinnerPosition = myAdap.getPosition(myString);

//set the default according to value
mySpinner.setSelection(spinnerPosition);

From: How to set selected item of Spinner by value, not by position?

Community
  • 1
  • 1
BZ.
  • 1,928
  • 2
  • 17
  • 26