How to get selected item of a singlechoice Alert Dialog?

I have this code to show a dialog with singlechoice(radio) options.

AlertDialog ad = new AlertDialog.Builder(this)
.setCancelable(false)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.choose_one)
.setSingleChoiceItems(seq, pos,null)
.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() { 
  public void onClick( DialogInterface dialog, int whichButton) 
  { 
    // dialog dismissed
  } 
 }).create();

How do I get the choice that has been selected?


Solution 1:

I know this is an old post, but i just came across it, and found that this solution seems at bit more simple that whats been posted here.

You can just do like this:

In your onClick() handler on the dialog positive button, add the following code:

ListView lw = ((AlertDialog)dialog).getListView();
Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());

Note that if you haven't selected any option it will crash you have to add a check here before getting the checkedItem with if(lw.getCheckedItemCount() > 0)

Solution 2:

I tried to use ListView.setSelection(int) but it never worked as expected so instead I decided to make use of View.setTag() to temporarily store the selected position.

.setSingleChoiceItems(adapter, -1,
        new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ListView lv = ((AlertDialog)dialog).getListView();
            lv.setTag(new Integer(which));
        }
})

The tag can then be accessed easily after a button click.

.setPositiveButton(R.string.button_text,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        ListView lv = ((AlertDialog)dialog).getListView();
        Integer selected = (Integer)lv.getTag();
        if(selected != null) {
            // do something interesting
        }
    }
})

Solution 3:

I believe that you use an OnClickListener for setSingleChoiceItems(), to listen whenever an item has been selected; then once the user hits okay, you set that item in stone. Right now you're just passing null, so nothing you can't pick up which item was selected.

Solution 4:

1). create Array.

final ArrayList<String> arrData = new ArrayList<String>();

2). add data to array you have created.

if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {

                    arrData .add(cursor.getString(1)); 
                                             // (1 = int columnIndex)

                } while (cursor.moveToNext());
            }
        }

3). get data like this.

public void onClick(DialogInterface dialog, int item) {

                        Log.d("Selected", arrData.get(item) + "");

                    }

4). that's all :)

Solution 5:

You can do just like this in on onClick() method

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.edit_set_waiting_period)
                .setItems(R.array.str_set_waiting_period, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                        L.e("selectedItmes", which + "");

                        ListView lw = ((AlertDialog) dialog).getListView();
                        Object checkedItem = lw.getAdapter().getItem(which);
                        L.e("checkedItem", checkedItem.toString() + "");

                    }
                });

        builder.show();