android - OnItemSelectedListener 不适用于微调器

标签 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" />

问题是,无论何时从微调器中选择一个项目,都不会发生任何事情,即使在我删除了所有条件之后,如您所见。

最佳答案

OnItemSelectedListener not working for spinner

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

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

关于android - OnItemSelectedListener 不适用于微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845424/

相关文章:

如果在下拉菜单之外点击,Android Spinner 监听器

android - 微调器弹出窗口标题背景

NHibernate PreUpdate 事件监听器不持久化更改

java - Java 中组件的 componentResized 事件,但仅在鼠标释放时执行

android - 如何从应用程序移动到动态壁纸预览?

android - 如何以编程方式使 TableRow 居中

java - 在一个匿名类中实现两个接口(interface)

java - 只获取html中的href内容和src内容

android - 如何将项目添加到动态创建的微调器?

java - 以编程方式创建布局并设置 ClickListener(android)