Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 358
Pentium10 Avatar asked Mar 22 '10 17:03

Pentium10


People also ask

What are the valid way of containing items in an alert dialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

What is the difference between alert dialog and dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How can I display a list view in an Android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.

How do I add a check box to an alert in dialog?

The way to make a checkbox list is to use setMultiChoiceItems in the AlertDialog . // Set up the alert builder AlertDialog. Builder builder = new AlertDialog. Builder(context); builder.


2 Answers

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)

like image 128
Dan Avatar answered Sep 28 '22 01:09

Dan


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         }     } }) 
like image 23
Matt Briançon Avatar answered Sep 28 '22 00:09

Matt Briançon