42

I haven't been able to set a Single Choice list, or a Multiple Choice List inside an AlertDialog.

I tried following the examples but I only get a Dialog with a Title, the Ok and Cancel buttons, and no list, and NO message (which I set!).

Here is the code:

    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DELETE_CITY:
        CharSequence[] array = {"Red", "Blue", "Yellow"}; 
        return new AlertDialog.Builder(ShowPypData.this)
            .setTitle(R.string.city_actions_delete_label)
            .setMessage(R.string.city_actions_delete_select_label)
            .setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }

            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

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

                }
            }).create();
    default:
        return null;
    }

}

The weird thing is that if I comment the setSingleChoiceItems part, I can see the message on the dialog.

Julian Suarez
  • 4,499
  • 4
  • 24
  • 40

2 Answers2

49

Seems that Buttons, Message and Multiple choice items are mutually exclusive. Try to comment out setMessage(), setPositiveButton() and setNegativeButton(). Didn't check it myself.

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
slkorolev
  • 5,883
  • 1
  • 29
  • 32
  • 9
    you are completely right, setMessage, setSingleChoiceItems and setMultiChoiceItems are all mutually exclusive. Buttons instead can be used with all kinds of AlertDialog. So thank you! – Julian Suarez Oct 22 '11 at 17:59
  • 3
    From http://developer.android.com/guide/topics/ui/dialogs.html "Because the list appears in the dialog's content area, the dialog cannot show both a message and a list and you should set a title for the dialog with setTitle()." – SoloPilot Apr 05 '14 at 15:41
  • It may help http://stackoverflow.com/questions/5660887/how-to-select-a-entry-in-alertdialog-with-single-choice-checkbox-android – jettimadhuChowdary Oct 19 '16 at 08:19
5

this code works for me

 final CharSequence[] charSequence = new CharSequence[] {"As Guest","I have account here"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Buy Now")
                //.setMessage("You can buy our products without registration too. Enjoy the shopping")
                .setSingleChoiceItems(charSequence, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        utility.toast(" "+charSequence);
                    }
                })
        .setPositiveButton("Go", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show()
saigopi.me
  • 14,011
  • 2
  • 83
  • 54