0

i have a radio group with two radio buttons in it. I want to get the value of the radio button and then store it in the database..how do i do that?? Pls help! I searched for it but all in vain! I tried this code but my activity stops working after using it

rg=(RadioGroup)findViewById(R.id.radioGroup2);
if(rg.getCheckedRadioButtonId()!=-1)
    {
        int id=rg.getCheckedRadioButtonId();
        View radioButton=rg.findViewById(id);
        int radioid=rg.indexOfChild(radioButton);
        RadioButton btn = (RadioButton) rg.getChildAt(radioid);
        Father_spouse=(String)btn.getText();
    }
shivani
  • 746
  • 3
  • 22
  • 40

1 Answers1

2
  1. if you want to store the text label of your RadioButton then use this :

    // get selected radio button from radioGroup
    int selectedId = radioGroup.getCheckedRadioButtonId();
    
    if(selectedId != -1) {    
       // find the radiobutton by returned id
       selectedRadioButton = (RadioButton) findViewById(selectedId);
    
       // do what you want with radioButtonText (save it to database in your case)
       String radioButtonText = selectedRadioButton.getText();
    }
    
  2. if you want to save a boolean value so test on the selectedId of your RadioButtons and save a 0 or 1 to your database column (Example of two radio buttons to enable/disable updates) :

    // get selected radio button from radioGroup
    int selectedId = radioGroup.getCheckedRadioButtonId();
    boolean isAllowUpdate = false;
    switch(selectedId) {
        case R.id.radioAllowUpdate : isAllowUpdate = true; break;
        case R.id.radioDisableUpdate : isAllowUpdate = false; break;
    }
    
    //save it to database 
    if(isAllowUpdate)
       // true ==> save 1 value
    else 
       // false ==> save 0 value
    

EDIT :

if you should control the selected value and when send it to database, see this tutorial

Houcine
  • 24,001
  • 13
  • 56
  • 83
  • i tried this code..have a look..`rg=(RadioGroup)findViewById(R.id.radioGroup2); int selected=rg.getCheckedRadioButtonId(); rb=(RadioButton)findViewById(selected); Father_spouse=(String) rb.getText();` it does not store the value i checked..it stores the value that is already checkd wen the activity loads.. – shivani Mar 29 '13 at 11:45
  • this is normal because you are putting this code in your `onCreate()` method , so the default selected value will be stored in database, to achieve that, try to add a Button and put your code in `onClick()` method, see my edits – Houcine Mar 29 '13 at 11:49
  • selectedradiobutton and -1 are two different datatypes..it is showing error – shivani Mar 29 '13 at 11:57
  • sorry i've made a simple stupid mistake , the `if` clause should be before , see my edits :) – Houcine Mar 29 '13 at 12:16
  • does keeping the checked option for radio button in xml file make any difference??? – shivani Mar 29 '13 at 12:19
  • no ,it is just to tell the app that the X radioButton should be checked by default. – Houcine Mar 29 '13 at 12:19