如何更新AlertDialog中的列表视图

发布于 2024-12-06 09:30:14 字数 1662 浏览 1 评论 0原文

我有一个包含列表视图的对话框,列表视图的数据来自对话框的父级。每次要显示对话框时,它都应该从活动中获取数据来构建其列表项。我知道我应该在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻想少年梦 2024-12-13 09:30:14

我正在为 listview 创建一个自定义适配器..这样做:

  dialog2 = new Dialog(SActivity.this);
        ListView modeList = new ListView(SActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(SActivity.this);

            builder.setTitle(" resul[s] ");
            MySimpleAdapter adapter = new MySimpleAdapter(SActivity.this, data , R.layout.list_main, 
                    new String[] { "name", "distance" ,"phone","web"}, 
                    new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
            modeList.setAdapter(adapter);





        builder.setView(modeList);
        dialog2 = builder.create();
        dialog2.show();

I am creating a custom adapter for listview..doing like this :

  dialog2 = new Dialog(SActivity.this);
        ListView modeList = new ListView(SActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(SActivity.this);

            builder.setTitle(" resul[s] ");
            MySimpleAdapter adapter = new MySimpleAdapter(SActivity.this, data , R.layout.list_main, 
                    new String[] { "name", "distance" ,"phone","web"}, 
                    new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
            modeList.setAdapter(adapter);





        builder.setView(modeList);
        dialog2 = builder.create();
        dialog2.show();
苏别ゝ 2024-12-13 09:30:14

简单的答案:调用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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文