如何动态地在Android的正确更新的微调? [英] How to update an Spinner dynamically in Android correctly?

查看:116
本文介绍了如何动态地在Android的正确更新的微调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库中显示对象自定义适配器微调。
当对象列表改变了我创建的列表中的一个新的适配器,并将其应用在离心机。之后的第一个项目被选中,所以我尝试这样的:

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

不过,GUI也仍然显示的第一个项目?!

But the GUI does still show the first item?!

spinner.invalidate() did not help.

这是正确的方式来实现我想要什么?我真的无法找到这种行为的任何信息。任何帮助将大大AP preciated!
问候

Is this the correct way to achieve what I want? I really could not find any information on this behavior. Any help would be greatly appreciated! Regards

推荐答案

解决:我想主要问题是自定义的微调适配器。现在这工作得很好。

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

这篇关于如何动态地在Android的正确更新的微调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆