如何更新AlertDialog中的列表视图
我有一个包含列表视图的对话框,列表视图的数据来自对话框的父级。每次要显示对话框时,它都应该从活动中获取数据来构建其列表项。我知道我应该在 onPrepareDialog() 中执行此操作,但我不知道如何更新 AlertDialog.builder 创建的列表视图,有人可以帮助我吗?
我的对话框是由以下代码创建的:
new AlertDialog.Builder(this)
.setTitle(title)
.setMultiChoiceItems(cityNames(), updateSelections(),
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
if(isChecked){
selections[whichButton] = true;
// Toast.makeText(MainActivity.this, cities.get(whichButton).getName(), Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
for(int i=0;i<selections.length;i++){
if(selections[i]==true){
removeCityFromScreen(i);
}
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
I have a dialog that contains a listview, the data of the list view comes from the parent of the dialog. Every time the dialog is about to be displayed, it should get data from the activity to build its list items. I know I should do this in onPrepareDialog(), but I don't know how to update the listview created by AlertDialog.builder, Could anyone help me?
My dialog is created from below code:
new AlertDialog.Builder(this)
.setTitle(title)
.setMultiChoiceItems(cityNames(), updateSelections(),
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
if(isChecked){
selections[whichButton] = true;
// Toast.makeText(MainActivity.this, cities.get(whichButton).getName(), Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
for(int i=0;i<selections.length;i++){
if(selections[i]==true){
removeCityFromScreen(i);
}
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在为 listview 创建一个自定义适配器..这样做:
I am creating a custom adapter for listview..doing like this :
简单的答案:调用
create()
后,没有机会更新列表。构建器会扩充 XML 并在调用时创建自定义适配器。我建议您每次要显示对话框时都创建/构建对话框。注意:当然,您可以提供具有自己的多选实现的自定义
AlertDialog
设置。然后您可以轻松更新它,因为您可以访问列表的适配器(比较 这个答案 - 在这种情况下您可以创建并设置一个新的适配器)。Simple answer: After calling
create()
there's no chance to update the list. The builder inflates XML's and creates custom adapters when calling that. I would recommend to create / build the dialog every time you're about to display it.Note: Af course you could provide a custom
AlertDialog
setup with an own multi choice implementation. Then you could easily updated it since you have access to the adapter of the list (compare this answer - you could create and set a new adapter in this case).