2017-06-07 61 views
0

我使用下面的代碼來設置微調值的選定項目的微調。但是我得到的位置是-1。要設置所選項目的微調值,返回-1

代碼:

final ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1, location_days2); 
     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     location_days.setAdapter(adapter1); 
     String b = "5 Days"; 
     int a = adapter.getPosition(b); //this should return 5, but returns -1 
     location_days.setSelection(a); 

字符串數組:

私有String [] location_days2 = { 「選擇天」, 「1天」, 「2天」, 「3天」 ,「0天」,「5天」,「6天」};

回答

1
int a = adapter.getPosition(b); 

適配器1在上述行更換適配器,因爲你已經初始化適配器作爲「適配器1」

+0

呀它現在工作。這是一個愚蠢的@sathiya –

0

你可以做這樣的事情來設置選擇在轉

int pos = 0; 
String[] location_days2 = {"Select days", "1 Day", "2 Days", "3 Days", "0 Days", "5 Days", "6 Days"}; 
for (String location : location_days2) { 
    if (location.equalsIgnoreCase("5 Days")) { 
     pos = Arrays.asList(location_days2).indexOf("23"); 
     break; 
     } 
    } 
location_days.setSelection(pos);