2014-02-18 29 views
1

我有我已經設置了使用定製ArrayAdapter旋:OnItemSelectedListener不工作的噴絲

private static class CustomAdapter<T> extends ArrayAdapter<String> { 
    public CustomAdapter(Context context, int textViewResourceId, String[] objects) { 
     super(context, textViewResourceId, objects); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     TextView textView = (TextView) view.findViewById(android.R.id.text1); 
     textView.setText(""); 
     return view; 
    }  

據如下初始化(旋轉器旋轉器;語句是向上以上爲類變量):

this.spinner = (Spinner) findViewById(R.id.spinner1); 
    CustomAdapter<String> adapter = new CustomAdapter<String>(this, 
     android.R.layout.simple_spinner_dropdown_item, new String[] {"Set Homepage"}); 

    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(this); 

我已經實現了OnItemSelectedListener:

public class MainActivity extends Activity implements OnItemSelectedListener{...} 

,並有要求回調:

//spinner methods 
@Override 
public void onItemSelected(AdapterView<?> parent, View view, 
     int pos, long id) { 
    // TODO Auto-generated method stub 
    //if (pos == 1){ 
     Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show(); 
    //} 
} 

@Override 
public void onNothingSelected(AdapterView<?> parent) { 
    // TODO Auto-generated method stub 
    //Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show(); 

} 

對於微調的XML:

  <Spinner 
      android:id="@+id/spinner1" 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:background="@drawable/ic_menu_moreoverflow_holo_dark" /> 

的問題是,當一個項目從微調選擇,什麼都沒有發生,即使在我刪除了所有條件,你可以看到以上。

+1

如果可能,請將整個班級代碼放在一起。 – DcodeChef

+1

它差不多700行,所以你們,我不太確定它是如何有助於說實話,你有什麼想法? – timeshift117

+0

我在這段代碼中看不到任何錯誤。代碼/佈局的其餘部分可能是錯誤的。 –

回答

2

OnItemSelectedListener不工作的微調

由於您所傳遞的適配器是默認選中的只有一個項目。在啓動應用程序時,您可能會收到敬酒信息。

因此添加更多元素來檢查OnItemSelectedListener行爲。

+0

優秀!你是對的,它現在在工作,非常感謝。 – timeshift117