views:

331

answers:

1

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?

+1  A: 

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.

Daniel Lew
So you say I have to temporarily store the item when the event fires, and to save when the dialog is dismissed?
Pentium10
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.
Daniel Lew