Android微调 – onItemSelected / setOnItemSelectedListener不触发

这使我疯狂,因为这是我以前做的事情,但无法弄清楚为什么现在不工作…

我有一个菜单button,通过menu.xml文件和带有开关的onOptionsItemSelected方法以通常的方式实现,它创build并显示一个微调

我已经添加了setOnItemSelectedListener ,但它似乎从来没有触发。 微调出现,我select一个选项或退出,无论onItemSelectedonNothingSelected调用。

这是菜单button处理切换语句的“case”和“return true”之间的所有代码。 ( topThis是一个引用该活动的上下文的variables – 适用于应用程序中的所有其他吐司)

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

有任何想法吗? 我隐约怀疑,我正在编程创buildSpinner的事实是问题…

Solutions Collecting From Web of "Android微调 – onItemSelected / setOnItemSelectedListener不触发"

我在实现微调时遇到了类似的问题,我通过获取父视图并设置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 } }); 

请记住,如果不添加一些自定义代码来处理微调器select,则无法重新select相同的微调器项目,它始终将第一个项目设置为选定项目。

对于Toast没有显示,我build议总是使用“MyActivity.this”作为你的上下文,当在一个监听器接口里面创build一个Toast时,像这样:

 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) 

最后的.show()很容易忘记;)

实际上,如果您的微调器可见性设置为不存在,那么当您调用performclick()方法时它将触发它的点击,但不会触发它的setOnItemSelectedListener,因此您需要更改可见性,然后才能工作

我知道这个问题有点老了,但是如果您正在等待AsyncTaskcallback,请确保您通过调用callback线程上的notifyDataSetChanged()来让适配器知道数据更改!

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