2012-07-25 174 views
6

我想在運行時設置android微調器項目值。Android微調器設置值

這是我到目前爲止有:

final ArrayAdapter<String> calsListAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, calendarNames); 
eventCalendarList.setAdapter(calsListAdapter); 
eventCalendarList.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     calendarChoosen = availableCals.get(arg2); 
    } 
    public void onNothingSelected(AdapterView<?> arg0) { 
     Logger.d("Cal Choosen", "fffffffffffffff"); 
    } 
}); 

private List<AvailableCalendar> availableCals = new ArrayList<AvailableCalendar>(); 
private AvailableCalendar calendarChoosen; 

但沒有設置值。如何做呢?

+0

張貼一些代碼。您的解釋不夠清楚,無法理解問題。 – 2012-07-25 18:44:51

+0

@DavidWasser代碼如下:final ArrayAdapter calsListAdapter = new ArrayAdapter ( this,android.R.layout.simple_list_item_1,calendarNames); \t eventCalendarList.setAdapter(calsListAdapter); \t \t \t eventCalendarList \t \t \t \t \t .setOnItemSelectedListener(新OnItemSelectedListener(){ \t \t \t \t \t \t公共無效onItemSelected(適配器視圖爲arg0, \t \t \t \t \t \t \t \t查看ARG1,ARG2 INT, long arg3){ \t \t \t \t \t \t \t calendarChoosen = availableCals.get(arg2); \t \t \t \t \t \t} \t \t \t \t \t \t公共無效onNothingSelected(適配器視圖爲arg0){ \t \t \t \t \t \t \t Logger.d( 「校準Choosen」, 「fffffffffffffff」); \t \t \t \t \t \t} \t \t \t \t \t}); – dominic 2012-07-26 06:05:18

+0

private List availableCals = new ArrayList (); private AvailableCalendar calendarChoosen; – dominic 2012-07-26 06:18:48

回答

10

我打算假設您在此創建基於List<CharSequence>或類似的微調器的適配器。您可以使用,這樣做對選擇查找,如:

String name = model.getName(); 
int index = list.indexOf(name); 
if (index != -1) spinner.setSelection(index); 

顯然,如果你的模型沒有包含任何「名稱」的數據,那麼就沒有在微調選擇,所以你可能需要添加一些邏輯來處理。如果模型確實具有「名稱」,則在用作適配器的數據源的原始列表中找到其索引。只有在發現事件時,才能將微調器的選擇設置爲同一索引。

+0

非常感謝...它像一個魅力 – 2014-02-14 10:38:57

2

我編程設定的微調值是這樣的:

public class MainActivity extends Activity { 
    private SharedPreferences prefs; 
    private String prefName = "spinner_value"; 
    int id=0; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final List<String> list=new ArrayList<String>(); 
     list.add("Item 1"); 
     list.add("Item 2"); 
     list.add("Item 3"); 

     final Spinner sp=(Spinner) findViewById(R.id.spinner1); 
     ArrayAdapter<String> adp= new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1,list); 
     adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     sp.setAdapter(adp); 

     prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
     id=prefs.getInt("last_val",0); 
     sp.setSelection(id); 

     sp.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, 
       View arg1,int pos, long arg3) { 

      prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefs.edit(); 
      //---save the values in the EditText view to preferences--- 
      editor.putInt("last_val", pos); 

      editor.commit(); 

      Toast.makeText(getBaseContext(), 
       sp.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub     
     } 
    });    
}