Skip to content Skip to sidebar Skip to footer

How To Set Position In Spinner?

I read with BufferedReader text from a system file; this text contains, for example, 5 WORDS, but in another case it can contain fewer or more words. Then I put this text (the ment

Solution 1:

Here is solution, thanks to sfratini for help.

Use:

spinner.setSelection(getIndex(spinner, myString));

Then:

privateintgetIndex(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;
}

Solution 2:

i think this line will help you

String[] a= newString[10];
 a[0]="abc";
 a[1]="xyz";
 a[2]="pqr";
 .....
 .....
 spin = (Spinner) findViewById(R.id.TimeSpinner);
 ArrayAdapter<String> adapter = newArrayAdapter<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);

Solution 3:

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 = newArrayList<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"));

Post a Comment for "How To Set Position In Spinner?"