26

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?

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
bugzy
  • 7,086
  • 9
  • 42
  • 44
  • You can also see this sample of alert dialog with listview go here [for demo example](http://goo.gl/W3a4h) – Herry Dec 06 '11 at 19:18

3 Answers3

92

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.

Gatekeeper
  • 6,708
  • 6
  • 27
  • 37
Erich Douglass
  • 51,744
  • 11
  • 75
  • 60
  • 5
    just put dismiss() in the public void onClick() function will dismiss the dialog for you. – Yenchi Jun 23 '10 at 19:04
0

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

Community
  • 1
  • 1
0

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();
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300