2014-08-28 3 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.setOnItemSelectedListener(this); 

이 한 번만 수행해야합니다, 스피너가 생성됩니다

spinner.setSelection(0); 

그리고는 리스너를 설정합니다. 이렇게하면 기본적으로 한 번만 호출되는 OnItemSelectedListener으로 불필요한 전화가 걸리지 않게됩니다.

관련 문제