0

I have a radio group with 2 options which is Male and Female. I wanted to save RadioButton Male as int number "1" and Female as "2" into my database. But I don't really know how to implement it. If it is possible could someone enlighten me on this issue? Thank you in advance.

    <RadioGroup
    android:id="@+id/radio_sex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/email_address"
    android:layout_alignTop="@+id/textView5"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radio_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
</RadioGroup>
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
  • have you considered fetching the index of the RadioButtons in you RadioGroup? If not, maybe you need to see this [question](http://stackoverflow.com/questions/6440259/how-to-get-the-selected-index-of-a-radiogroup-in-android). – Lokesh Mehra Nov 19 '12 at 09:17
  • Thanks Lokesh Mehra, I got some pretty useful info in that question. – IssacZH. Nov 19 '12 at 09:28

3 Answers3

3
RadioGroup rg = (RadioGroup) findViewById(R.id.radio_sex); 
int selected = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selected);
if(rb.getText().equals("Male"){
  //save to db number one
}else{
  //save number two
}
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
3

Set tag for radioButton Male as 1 and female as 2.

<RadioButton
    android:id="@+id/radio_male"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="1"
    android:text="Male" />

 <RadioButton
    android:id="@+id/radio_female"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="2"
    android:text="Female" />

Then on checked changed, get the tag of the selected Radio Button and cast it to int and then save to database.

 setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int value=Integer.parseInt(findViewById(checkedId).getTag().toString());
        }
    });
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • I managed to solve my issue already, but still thank you for the answer. This gives me an alternative way that I can apply to solve my issue. – IssacZH. Nov 19 '12 at 09:35
2
<RadioGroup
    android:id="@+id/radio_sex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/email_address"
    android:layout_alignTop="@+id/textView5"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radio_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
</RadioGroup>

now in your java file check either of the radio buttons

int maleFemaleVar = ( radio_male.isChecked() ? 1 : 2 );

now save this new maleFemaleVar to your DB