0

I'm using alert dialog box so that the user can select one of the option from the list in android. Now the problem i'm facing is that i don't know how to check a default radio button in the starting and even after user selects any of the option from the radio button I don't know how to save the state of radio button. I'm using this code:

private void SingleChoiceWithRadioButton() { 
         final String[] selectFruit= new String[]{"Blacklist","Whitelist"};
         Builder builder = new AlertDialog.Builder(this);  
         builder.setTitle("Single Choice With Radio button");  
         builder.setSingleChoiceItems(selectFruit, -1,  
             new DialogInterface.OnClickListener() {  
               @Override  
               public void onClick(DialogInterface dialog, int which) { 

                 Toast.makeText(callBlockerSettings.this, selectFruit[which]+":"+ which + " Selected", Toast.LENGTH_LONG).show();  
               //  dialog.dismiss();  
               }  
             });  
         builder.setPositiveButton("ok",  
             new DialogInterface.OnClickListener() {  
               @Override  
               public void onClick(DialogInterface dialog, int which) {  
                 dialog.dismiss();  
               }  
             });  
         AlertDialog alert = builder.create();  
         alert.show();  
       } 
deathnote
  • 95
  • 1
  • 3
  • 11

2 Answers2

2

As the developer guide suggests

public AlertDialog.Builder setSingleChoiceItems (int itemsId, int checkedItem, DialogInterface.OnClickListener listener)

Parameters itemsId the resource id of an array i.e. R.array.foo checkedItem specifies which item is checked. If -1 no items are checked. listener notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

   public int selectedElement=-1; //global variable to store state
   private AlertDialog alert;
   private void SingleChoiceWithRadioButton() { 
             final String[] selectFruit= new String[]{"Blacklist","Whitelist"};
             Builder builder = new AlertDialog.Builder(this);  
             builder.setTitle("Single Choice With Radio button");  
             builder.setSingleChoiceItems(selectFruit, selectedElement,  
                 new DialogInterface.OnClickListener() {  
                   @Override  
                   public void onClick(DialogInterface dialog, int which) { 
                       selectedElement=which;
                     Toast.makeText(callBlockerSettings.this, selectFruit[which]+":"+ which + " Selected", Toast.LENGTH_LONG).show();  
                   //  dialog.dismiss();  
                   }  
                 });  
             builder.setPositiveButton("ok",  
                 new DialogInterface.OnClickListener() {  
                   @Override  
                   public void onClick(DialogInterface dialog, int which) {  
                     dialog.dismiss();  
                   }  
                 });  
             alert = builder.create();  
             alert.show();  
           } 

//Call this method always

   private void showDialog(){
      if(alert==null)
       SingleChoiceWithRadioButton();
      else
       alert.show();
   }

The -1 here is the default selected item index (-1 means do not select any default). use this parameter to set the default selected.

If you need to store this permanently you could save this value(selectedElement) in SharedPreferences and then retrieve the same and initialize it during the onCreate() of your activity.

humblerookie
  • 4,717
  • 4
  • 25
  • 40
  • hey, i got what u said but how are u setting the radio button values. Suppose i have a items in the list with radio buttons and i'm storing the index of the button that is clicked by the user in variable X. Then how are u going to set it when next time i open alertdialog box. – deathnote Aug 21 '14 at 19:32
  • check updated answer. If you wish to store the value permanently check how to store and retrieve it in/from shared preferences – humblerookie Aug 21 '14 at 19:46
1

In your code, in this method:

public void onClick(DialogInterface dialog, int which)

you should save the which value, as it represents the selected current value. Use a member of your class to store or SharedPreferences, what you see fit.

//Class initialization

int my_previous_selected = -1;

//... code

public void onClick(DialogInterface dialog, int which) { 
  my_previous_selected = which;
    }  
  }); 

If you want to reopen your Dialog keeping the same value checked, you have to use your setSingleChoiceItems second parameter as follows:

builder.setSingleChoiceItems(selectFruit, my_prevous_selected,  
         new DialogInterface.OnClickListener() {  

//... code

Hope it helps

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • hey, i got what u said but how are u setting the radio button values. Suppose i have a items in the list with radio buttons and i'm storing the index of the button that is clicked by the user in variable X. Then how are u going to set it when next time i open alertdialog box. – deathnote Aug 21 '14 at 19:33
  • You could store it in your sharedpreferences, check this link. http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – zozelfelfo Aug 21 '14 at 19:52