Java Spinner 的 OnItemClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21600781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:36:41  来源:igfitidea点击:

OnItemClickListener of spinner

javaandroidxmlandroid-layoutandroid-spinner

提问by bShah

I am able to get Spinner in action bar this way; array of items in re/values/languages.xml

我可以通过这种方式在操作栏中获得 Spinner;re/values/languages.xml 中的项目数组

 <string-array name="languages">
    <item>Finnish</item>
    <item>French</item>
    <item>German</item>
    <item>Slovakian</item>
    <item>Polish</item>
</string-array>

In res/menu/main.xml

在 res/menu/main.xml

  <item
    android:id="@+id/menuSort"
    android:actionLayout="@layout/spinner"
    android:showAsAction="ifRoom"
    android:title="@string/choose"/>

In res/layout/spinner.xml

在 res/layout/spinner.xml

<Spinner
    android:id="@+id/spinner"
    android:layout_width="150dp"
    android:layout_height="wrap_content" 
    android:entries="@array/languages"/>

And finally activity class;

最后是活动课;

 public class Base_Activity extends Activity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    final Spinner spinner = new Spinner(this);
    Log.i("DEBUG1", "CHECKPOINT1");
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);
    Log.i("DEBUG2", "CHECKPOINT2");
    spinnerArrayAdapter.setDropDownViewResource(0);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

               String items=spinner.getSelectedItem().toString();
               Log.i("Selected item : ",items);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });
    return true;
}

}

}

What I am not getting is on ItemSelected of spinner items nothing happens. As you can see from my xml file that I do not even need Adapter to get the spinner. But I am using adapter to so something to get OnItemSelected. Please help me what has happened here?

我没有得到的是 ItemSelected 的微调项目没有任何反应。从我的 xml 文件中可以看出,我什至不需要 Adapter 来获取微调器。但是我正在使用适配器来获取 OnItemSelected。请帮我看看这里发生了什么?

采纳答案by Biraj Zalavadia

try this may helps you

试试这可能对你有帮助

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        final Spinner spinner = (Spinner) menu.getItem(0).getActionView().findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String items = spinner.getSelectedItem().toString();
                Log.i("Selected item : ", items);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });
        return super.onCreateOptionsMenu(menu);
    }

回答by Shashank Kadne

You have to add OnItemClickListenerand not OnItemSelectedListenerif you want the click event.

如果你想要点击事件,你必须添加OnItemClickListener而不是OnItemSelectedListener

Your method will only work if you select your items or browse through the view using a trackball or up/down arrows. If you want click, then add OnItemClickListenerand override

您的方法仅在您选择项目或使用轨迹球或向上/向下箭头浏览视图时才有效。如果你想点击,然后添加OnItemClickListener和覆盖

public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3)