Android – Update data in an AlertDialog?

So, if I create an AlertDialog like so:

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

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?

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.

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.


Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Android – Update data in an AlertDialog?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录