简体   繁体   English

Android Spinner - onItemSelected / setOnItemSelectedListener未触发

[英]Android Spinner - onItemSelected / setOnItemSelectedListener not triggering

This is driving me nuts since it's something I've done before but can't figure out why it isn't working now... 这让我疯了,因为这是我以前做过的事情,但是无法弄清楚为什么它现在不起作用......

I've got a menu button, implemented in the usual way via a menu.xml file and the onOptionsItemSelected method with a switch in it, that creates and displays a spinner. 我有一个菜单按钮,通过menu.xml文件以及带有开关的onOptionsItemSelected方法以通常的方式实现,创建并显示一个微调器。

I've added the setOnItemSelectedListener , but it never seems to trigger. 我已经添加了setOnItemSelectedListener ,但它似乎永远不会触发。 The spinner appears, I pick an option or back out, neither onItemSelected or onNothingSelected are called. 旋转器出现,我选择一个选项或退出,调用onItemSelectedonNothingSelected

Here is all the code between the "case" and "return true" of the menu-button-handling switch statement. 这是菜单按钮处理开关语句的“case”和“return true”之间的所有代码。 ( topThis is a variable referring to the context of the activity - works fine for all other toasts in the app) topThis是一个引用活动上下文的变量 - 适用于应用程序中的所有其他toast)

String[] widgetModes = {"Mode 1", "Mode2"};
ArrayAdapter<String> widgetModeAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, widgetModes);
widgetModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner widgetModeSpinner = new Spinner(this);
widgetModeSpinner.setAdapter(widgetModeAdapter);
widgetModeSpinner.setPrompt("Choose Widget Mode");

widgetModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        Toast.makeText(topThis, "derp", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
    }
});

widgetModeSpinner.performClick();

Any ideas? 有任何想法吗? I vaguely suspect that the fact I'm creating the Spinner programmatically is the problem... 我隐约怀疑我以编程方式创建Spinner的事实是......

I had the similar problem when I was implementing a spinner, I resolved it by getting the parent View and set Adapter- 当我实现一个微调器时,我遇到了类似的问题,我通过获取父视图并设置适配器来解决它

spinner1 =(Spinner)findViewById(R.id.spinner1);
spinner1.setAdapter(BindSpinner("ProgramMaster",cols,null,true,""));
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
{
protected Adapter initializedAdapter=null;
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        if(initializedAdapter !=parentView.getAdapter() ) {
            initializedAdapter = parentView.getAdapter();
           return;
        }

        String selected = parentView.getItemAtPosition(position).toString();

        if(abc.equals("Select") && !selected.equals("Select"))
        {
            do something
        }

        else 
        {
            Do something
        }
        textQualification=selected;
        SearchUpdated("Qualification");
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }
});

Remember that you can't re-select the same spinner item, it always sets the first item as selected if you are not adding some custom code to handle the spinner selection. 请记住,如果不添加一些自定义代码来处理微调器选择,则无法重新选择相同的微调器项,它始终将第一个项设置为选中。

For the Toast not showing, I would suggest to always use the "MyActivity.this" as your context when creating a Toast inside a listener interface like this: 对于没有显示的Toast,我建议在侦听器界面中创建Toast时始终使用“MyActivity.this”作为上下文:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        /**
         * Called when a new item is selected (in the Spinner)
         */
         public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {
                // An spinnerItem was selected. You can retrieve the selected item using
                // parent.getItemAtPosition(pos)

                Toast.makeText(MyActivity.this, "Hello Toast",Toast.LENGTH_SHORT).show();

            }

            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing, just another required interface callback
            }

    }); // (optional)

And the .show() at the end is easy to forget sometimes;) 最后的.show()有时会很容易忘记;)

实际上,如果您的微调器可见性设置为已消失 ,那么当您调用performclick()方法时它将触发它的单击,但它不会触发其setOnItemSelectedListener,因此您需要更改可见性然后它将起作用

I know the question is a bit old, but in case you are waiting for a AsyncTask callback, make sure that you let your adapter know of the data changes by calling notifyDataSetChanged() on the callback thread! 我知道这个问题有点旧,但是如果你在等待AsyncTask回调,请确保通过在回调线程上调用notifyDataSetChanged()让你的适配器知道数据的变化!

@Override
public void onPostExecute(String result) {
   ///do something with your data  
   spinnerArrayAdapter.notifyDataSetChanged();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM