How to set position in spinner?
Here is solution, thanks to sfratini for help.
Use:
spinner.setSelection(getIndex(spinner, myString));
Then:
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;}
i think this line will help you
String[] a= new String[10]; a[0]="abc"; a[1]="xyz"; a[2]="pqr"; ..... ..... spin = (Spinner) findViewById(R.id.TimeSpinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(TimeSpin.this, android.R.layout.simple_spinner_item, a); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(adapter); spin.setSelection(0);
You don't have to use Adapter... you just need to convert String array to List
And then use indexOf(Object object) to get the index of you spin using selected color
String [] strings = yourString.split(" ");List<String> strList = new ArrayList<String>(Arrays.asList(strings));//Or you can load array from xml...//List<String> strList2 = Arrays.asList(getResources().getStringArray(R.array.array_color));spinner.setSelection(strList.indexOf("colorvalue"));