ListView in AlertDialog

前端 未结 3 589
傲寒
傲寒 2020-11-30 06:09

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

相关标签:
3条回答
  • 2020-11-30 06:21

    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 page has some other examples of different types of Dialogs.

    0 讨论(0)
  • 2020-11-30 06:30

    Used below code to display custom list in 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();
    
    0 讨论(0)
  • 2020-11-30 06:31

    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

    0 讨论(0)
提交回复
热议问题