Android的 - 在AlertDialog刷新数据? [英] Android - Refresh data in an AlertDialog?

查看:943
本文介绍了Android的 - 在AlertDialog刷新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果我创建一个AlertDialog像这样:

So, if I create an AlertDialog like so:

AlertDialog.Builder b = new AlertDialog.Builder();
b.setItems(MyStringArray, MyListener);
b.create().show();

和然后我想更新在列表中的项目,即,MyStringArray已更改为具有更多或更少的项目。我似乎无法找到一个方法来做到这一点。到目前为止,我已经尝试从AlertDialog得到的ListView,但我似乎无法得到.setAdapter工作。这是正确的做法,还是有更好的方法来做到这一点?

And then I want to update the items in the list, i.e. MyStringArray has changed to have more or fewer items. I can't seem to find a way to do this. So far, I've tried getting the ListView from the AlertDialog, but I can't seem to get .setAdapter to work. Is this the right approach, or is there a better way to do this?

推荐答案

我还没有尝试过这一点我自己,而是来自所有其他应用程序,我建我pretty肯定,这将解决您的问题。

I haven't tried this out myself, but from all the other apps I've built I'm pretty sure this will solve your problem.

而不是使用setItems的,​​请尝试使用setAdapter方法​​,并将已经从你的字符串数组初始化与数据的ArrayAdapter。然后,当你知道数据已经改变,你可以用getListView,让您的视图对象,并从那里调用getAdapter所以说现在你的数据直接合作。可以清除它,如果你想重新初始化,或者只是添加/删除的项目,只要你喜欢。从适配器对象,如果调用notifyDataSetChanged它使用新的数据集,你只是提供给适配器应触发重新绘制。

Instead of using setItems, try using the setAdapter method and pass in an ArrayAdapter that has been initialized with the data from your Array of String. Then, when you know that the data has changed, you can use getListView to get your View object and from there call getAdapter so that now you're working directly with the dataset. You can clear it, and re-initialize it if you like, or just add / remove the items as you like. From the adapter object, if you call notifyDataSetChanged it should trigger a re-draw using the new data set that you just supplied to the adapter.

希望有所帮助你。让我知道,如果它没有。

Hope that helps you out. Let me know if it doesn't.

DSC

这篇关于Android的 - 在AlertDialog刷新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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