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 callingsetAdapter():Obviously you can replace
"Watermelon"with something relevant for your app.