以编程方式添加OnItemSelectedListener的微调不会触发 [英] OnItemSelectedListener for Spinner that was added programmatically does not trigger
问题描述
我有打开编程好手。它弹出,并显示工作正常,但由于某些原因,我的OnItemSelectedListener并不在它触发任何事件。
I've got a spinner that opens programmatically. It pops up and appears to be working fine, but for some reason my OnItemSelectedListener does not trigger any of the events within it.
public class BeerConverter extends Activity {
ArrayAdapter<CharSequence> adapter3;
Spinner spinner03;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner03 = new Spinner(this);
adapter3 = new ArrayAdapter<CharSequence> (this, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner03.setAdapter(adapter3);
spinner03.setOnItemSelectedListener(new MyOnItemSelectedListener3());
adapter3.add("Stuff");
spinner03.performClick();
}
然后,我创建了监听器嵌套类:
Then I create the listener as a nested class:
public class MyOnItemSelectedListener3 implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id){
curPos = position;
Context context = getApplicationContext();
CharSequence text = "Test text. If you see this, it means MyOnItemSelectedListener3 was called.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
//do nothing
}
};
所以,当我发 spinner03.performClick();
微调正常弹出,但如果选择在微调的项目,它只是关闭,不调用该OnItemSelectedListener。它看起来像<一个href=\"http://stackoverflow.com/questions/5331626/create-spinner-without-spinner-in-xml/6635641#6635641\">this人有同样的问题的而回,但没有以往任何时候都结束了张贴的解决方案。
So when I send the spinner03.performClick();
the Spinner pops up correctly, but when an item in the Spinner is selected, it just closes and does not call the OnItemSelectedListener. It looks like this person had the same problem a while back, but didn't ever end up posting the solution.
推荐答案
如上评论跟帖表明,许多如果不是涉及到一个视图,你希望如果有问题的看法是不附加到一个窗口不会表现的所有事件。不要使用独立的视图来驱动任何种类的用户交互。
As the comment thread above suggests, many if not all events related to a View will not behave as you expect if the View in question is not attached to a window. Do not use unattached Views to drive any sort of user interaction.
这篇关于以编程方式添加OnItemSelectedListener的微调不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!