How to set position in spinner?

前端 未结 3 1998
花落未央
花落未央 2020-12-14 17:00

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 (t

相关标签:
3条回答
  • 2020-12-14 17:22

    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;
    }
    
    0 讨论(0)
  • 2020-12-14 17:32

    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"));
    
    0 讨论(0)
  • 2020-12-14 17:33

    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);
    
    0 讨论(0)
提交回复
热议问题