2014-08-28 168 views
1

我有一個問題,當我的微調訪問第一個案件,並立即重定向。我如何使用:Android微調,沒有選擇

@Override 
    public void onNothingSelected(AdapterView<?> view); { 
     // TODO Auto-generated method stub 

    } 

方法在用戶進行選擇之前正確保留在頁面上。以下是我的代碼。

// Creating adapter for spinner & choosing Drop down layout style - list view 
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.event,android.R.layout.simple_spinner_dropdown_item); 

    // attaching data adapter to spinner 
    spinner.setAdapter(adapter); 

    //spinner needs to know who is responsible for handling events 
    spinner.setOnItemSelectedListener(this); 
} 

@Override 
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) { 

    //casting the view to textView 
    TextView myText=(TextView) view; 

    // use .getText to display what text was selected by user 
    Toast.makeText(this,"You Selected "+myText.getText(),Toast.LENGTH_SHORT).show(); 



    switch (pos) { 
     case (0): 
      //Case selection redirecting user to 'Training Table' 
      Intent a = new Intent(Calendar.this, TrainingTable.class); 
      Calendar.this.startActivity(a); 
      break; 
     case (1): 
      //Case selection redirecting user to 'Race Table' 
      Intent b = new Intent(Calendar.this, Races.class); 
      Calendar.this.startActivity(b); 
      break; 

     case (2): 
      //Case selection redirecting user to 'Workshops page' 
      Intent c = new Intent(Calendar.this, Workshops.class); 
      Calendar.this.startActivity(c); 
      break; 
    } 

回答

2

強制微調,以選擇一個項目:

spinner.setSelection(0); 

,然後設置監聽器:

spinner.setOnItemSelectedListener(this); 

這隻需要做一次,在創建時微調。通過這種方式,您可以避免收到不需要的電話OnItemSelectedListener,默認情況下會調用一次。