Android AlertDialog 中的 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2397106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:42:34  来源:igfitidea点击:

ListView in AlertDialog

android

提问by bugzy

I am using a ListView in an AlertDialog to display a list of items. When the user clicks on one of the items, I want the dialog to close. I would not have any action buttons on the dialog. Any ideas on how I would accomplish this?

我在 AlertDialog 中使用 ListView 来显示项目列表。当用户单击其中一项时,我希望关闭对话框。我不会在对话框上有任何操作按钮。关于我将如何实现这一目标的任何想法?

回答by Erich Douglass

You should be able to do something like:

您应该能够执行以下操作:

final CharSequence[] items = {"Foo", "Bar", "Baz"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();

This pagehas some other examples of different types of Dialogs.

页面有一些其他不同类型对话框的示例。

回答by George Theodorakis

You can use a layout when popping an alert dialog.. It is easier to style it that way imo. For list in alert dialog you can do something like this

弹出警报对话框时,您可以使用布局.. imo 更容易设置它的样式。对于警报对话框中的列表,您可以执行以下操作

回答by Jawad Zeb

Used below code to display custom list in AlertDialog

使用下面的代码在 AlertDialog 中显示自定义列表

AlertDialog.Builder builderSingle = new AlertDialog.Builder(
                    DialogActivity.this);
            builderSingle.setIcon(R.drawable.ic_launcher);
            builderSingle.setTitle("Select One Name:-");
            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    DialogActivity.this,
                    android.R.layout.select_dialog_singlechoice);
            arrayAdapter.add("Hardik");
            arrayAdapter.add("Archit");
            arrayAdapter.add("Jignesh");
            arrayAdapter.add("Umang");
            arrayAdapter.add("Gatti");
            builderSingle.setNegativeButton("cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

            builderSingle.setAdapter(arrayAdapter,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String strName = arrayAdapter.getItem(which);
                            AlertDialog.Builder builderInner = new AlertDialog.Builder(
                                    DialogActivity.this);
                            builderInner.setMessage(strName);
                            builderInner.setTitle("Your Selected Item is");
                            builderInner.setPositiveButton("Ok",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            builderInner.show();
                        }
                    });
            builderSingle.create();
            builderSingle.show();