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 you can use that to loop thru and get the index. Another way is is to loop thru the adapter entries
1 2 3 4 5 6 7 |
Spinner s = (Spinner) findViewById(R.id.spinner_id); for(i=0; i < adapter.getCount(); i++) { if(myString.trim().equals(adapter.getItem(i).toString())){ s.setSelection(i); break; } } |