Android Spinner set Selected Item by Value
The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position. Here is an example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mySpinner.setSelection(getIndex(mySpinner, myValue)); private int getIndex(Spinner spinner, String myString){ int index = 0; for (int i=0;i<spinner.getCount();i++){ if (spinner.getItemAtPosition(i).equals(myString)){ index = i; } } return index; } |
If you are using an ArrayList for your Spinner Adapter then […]