OnItemSelectedListener不适用于微调器

问题描述:

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

I have a spinner which I have set up using a custom 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 spinner;上面的语句作为类变量):

It is initialized as follows (the Spinner spinner; statement is up above as a class variable):

    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:

I have implemented the 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" />

问题是,无论何时从微调器中选择一个项目,即使我删除了所有条件(如上所示),也没有任何反应.

The problem is, whenever an item is selected from the spinner, nothing at all happens, even after I removed all conditions as you can see above.

OnItemSelectedListener不适用于微调器

OnItemSelectedListener not working for spinner

因为您仅传递了默认情况下选中的适配器中的一项.启动应用程序时,您可能会收到吐司消息.

Because you are passing only one item in Adapter which is by default selected. Probably you are getting toast message when starting your Application.

因此,添加更多元素以检查OnItemSelectedListener行为.

So add more elements to check OnItemSelectedListener behavior.