Pentium10 Pentium10 -3 years ago 147
Android Question

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?

Answer Source

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download