如何重新创建对话框,包括其全部内容?
在我的应用程序中,我有 2 个选项卡。每个选项卡显示不同的活动。
在 Activity_1 中,我管理全局字符串数组 - 可以使用添加和删除选项。
在activity_2 中我有一个AlertDialog。 对话框是通过调用活动的对话框函数创建的:onCreateDialog、onPrepareDialog。 该对话框显示一个多项选择列表,该列表的源是 Activity_1 管理的全局数组。
问题:我想根据对activity_1 中的全局数组所做的更改,在activity_2 的对话框中显示更新的列表。问题是该列表是在 onCreateDialog 中创建的,并且该方法在活动的生命周期中仅被调用一次。
例如:如果全局数组包含:[“Banana”,“Orange”]并且在activity_1中我刚刚添加:“Apple”,然后单击选项卡#2,我希望activity_2打开对话框并显示在对话框列表:[“香蕉”、“橙色”、“苹果”]。
我尝试在 onPrepareDialog 中重新填充列表,但没有成功,它只是允许我决定要检查或不检查哪些列表项。我应该扩展 AlertDialog 吗?
任何帮助将不胜感激。
In my app I have 2 tabs. Each tab showing a different Activity.
In activity_1 I manage a global array of strings - add and delete options are available.
In activity_2 I have an AlertDialog.
The dialog is created by calling the Activity's dialog's functions: onCreateDialog, onPrepareDialog.
The dialog shows a multiple-choice list and the list's source is the global array that activity_1 manages.
The problem: I would like to display the updated list in the dialog of activity_2, according to the changes made to the global array in activity_1. The problem is that the list is created in onCreateDialog, and this method gets called only once in the activity's lifecycle.
For example: If the global array contains: ["Banana", "Orange"] and in activity_1 I've just added: "Apple", and I clicked tab #2, I would like activity_2 to open the dialog and display in the dialog list: ["Banana", "Orange", "Apple"].
I've tried to repopulate the list in: onPrepareDialog but with no success, it's just allowing me to decide which of the list items will be checked or not. Should I extend AlertDialog?
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 DialogInterface.OnShowListener 在每次显示时更新对话框内容。
编辑:
您必须实现一个 listadpater 并将适配器设置为构建器。
在 onPrepareDialog 中,获取列表视图
mAlertdlg.getListView()
并在其适配器上调用notifyDatasetChanged()
。或者再次设置适配器mAlertdlg.getListView().setAdapter(list)
You can use
DialogInterface.OnShowListener
to update the dialog content every time it gets displayed.Edit:
You will have to implement a listadpater and set the adapter to the builder.
In onPrepareDialog, get the listview
mAlertdlg.getListView()
and callnotifyDatasetChanged()
on its adapter. Or just set the adapter once againmAlertdlg.getListView().setAdapter(list)