OnItemSelectedListener不适用于微调器

时间:2014-02-18 05:53:29

标签: android spinner listener

我有一个使用自定义ArrayAdapter设置的微调器:

    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;
    }       

它初始化如下(Spinner微调器;语句在上面作为类变量):

    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 个答案:

答案 0 :(得分:4)

  

OnItemSelectedListener不适用于微调器

因为您在Adapter中只传递了一个默认选中的项目。在启动您的应用程序时,您可能会收到Toast消息。

因此,请添加更多元素来检查OnItemSelectedListener行为。

相关问题