如何动态更新Spinner? [英] How to update an Spinner dynamically correctly?
本文介绍了如何动态更新Spinner?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当对象列表更改时,我使用列表创建一个新的适配器并将其应用到微调框上。之后选择了第一个项目,所以我试过这个:
//选择第5项
int pos = spinner。 getSelectedItemPosition();
spinner.setAdapter(newAdapter);
//第0项选择
spinner.setSelectedItem(pos);
//选择第5项
但GUI仍然显示第一项? / p>
spinner.invalidate()
没有帮助。
解决方案
解决:我想主要的问题是自定义微调适配器。这样可以正常工作
if(spinner.getCount()> 0){
pos = spinner.getSelectedItemPosition );
}
MySpinnerAdapter adapter = new MySpinnerAdapter(context,myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); //需要
adapter.notifyDataSetChanged();
I have a spinner with a custom adapter displaying objects from a database. When the object list changed I create a new adapter with the List and apply it on the spinner. Afterwards the first item is selected, so I tried this:
// 5th item selected
int pos = spinner.getSelectedItemPosition();
spinner.setAdapter(newAdapter);
// 0th item selected
spinner.setSelectedItem(pos);
// 5th item is selected
But the GUI does still show the first item?
spinner.invalidate()
did not help.
Is this the correct way to achieve what I want? I really could not find any information on this behavior.
解决方案
Solved: I guess the main problem was the custom spinner adapter. This works fine now
if (spinner.getCount() > 0) {
pos = spinner.getSelectedItemPosition();
}
MySpinnerAdapter adapter = new MySpinnerAdapter(context, myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); // needed
adapter.notifyDataSetChanged();
这篇关于如何动态更新Spinner?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文