48

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?

Pentium10
  • 204,586
  • 122
  • 423
  • 502

7 Answers7

122

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)

Umair
  • 6,366
  • 15
  • 42
  • 50
Dan
  • 1,451
  • 1
  • 11
  • 8
  • 1
    Works like a charm! A well deserving +1. – Garima Tiwari May 13 '13 at 04:38
  • That's when closing the alertdialog. How do I retrieve it when the next time it's called? – Si8 Oct 13 '13 at 11:57
  • You want to get the value that was selected when the dialog was previusly opened, when the dialog is opened a second time, is that correct? In that case, can't you just set the selected value to a local variable, and then use that variable the next time you open the dialig? – Dan Nov 02 '13 at 09:47
  • lw.getCheckedItemPosition() is an extra step and should be replaced with 'which' variable as we already have it. – Umar Iqbal Oct 17 '14 at 12:44
  • Where do we have the 'which' variable? All I can see is the whichButton, and that's the button that has been clicked on in the AlertDialog. – Dan Oct 20 '14 at 10:54
  • 9
    Best solution for **Positive or Negative Listeners on AlertDialog**, since the **`which = -1`** in those cases. Thanks +1! – falvojr Jan 09 '15 at 17:39
  • 9
    Note that for radio button, it's `getCheckedItemPosition()` and NOT `getSelectedItemPosition()` which always returns `-1` - very confusing! – jakub.g Jun 15 '17 at 15:49
  • 1
    which is a bloody witch – Faizan Mubasher Dec 05 '17 at 12:29
  • Objet have what type? Integer (index) or String (literal). – luke cross Jul 21 '20 at 19:03
15

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
        }
    }
})
Matt Briançon
  • 1,064
  • 16
  • 24
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.

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • 2
    So you say I have to temporarily store the item when the event fires, and to save when the dialog is dismissed? – Pentium10 Mar 22 '10 at 17:17
  • I guess that theoretically you could do something fancier; I wonder if AlertDialog.getListView() would get you the choice list, then you could parse out which of those items is checked. But that seems like a lot more work than temporarily storing the item when the event fires. – Dan Lew Mar 22 '10 at 18:17
  • You can use `AlertDialog.getListView().getSelectedItem()` but that didn't seem to give me what I wanted. – Ian McLaird Nov 04 '11 at 15:14
1

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();
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
patel135
  • 927
  • 13
  • 19
1

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 :)

Bishan
  • 15,211
  • 52
  • 164
  • 258
0

position of the selected element is already given in the onClick method. so we can directly access the the array and get the selected item without any hassle. in this case: seq[which] will be the selected item.

priojeet priyom
  • 899
  • 9
  • 17
-8
EditText et = (EditText)findViewById(R.id.editText9);

int a = Integer.valueOf(item);

et.setText(items[a]);
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Dinesh
  • 1