本文介绍了Android Spinner - onItemSelected/setOnItemSelectedListener 未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯,因为这是我以前做过的事情,但不知道为什么现在不起作用......

This is driving me nuts since it's something I've done before but can't figure out why it isn't working now...

我有一个菜单按钮,通过 menu.xml 文件和带有开关的 onOptionsItemSelected 方法以通常的方式实现,用于创建和显示一个旋转器.

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.

我已经添加了 setOnItemSelectedListener,但它似乎永远不会触发.微调器出现,我选择一个选项或退出,onItemSelectedonNothingSelected 都没有被调用.

I've added the setOnItemSelectedListener, but it never seems to trigger. The spinner appears, I pick an option or back out, neither onItemSelected or onNothingSelected are called.

这是菜单按钮处理 switch 语句的case"和return true"之间的所有代码.(topThis 是一个引用活动上下文的变量 - 适用于应用程序中的所有其他 toast)

Here is all the code between the "case" and "return true" of the menu-button-handling switch statement. (topThis is a variable referring to the context of the activity - works fine for all other toasts in the app)

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();

有什么想法吗?我模糊地怀疑我以编程方式创建 Spinner 的事实是问题...

Any ideas? I vaguely suspect that the fact I'm creating the Spinner programmatically is the problem...

推荐答案

我在实现微调器时遇到了类似的问题,我通过获取父视图并设置适配器来解决它-

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

这篇关于Android Spinner - onItemSelected/setOnItemSelectedListener 未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:41