Search code examples
androidbundleandroid-radiogroupandroid-radiobutton

Sending values from 2 Radiogroups using Bundle passes only the second RadioGroup selected value


Hi I am stuck in a very strange code bug. Please Help!!!! My project has 2 Activities. 1) The First Activity is a form which includes 2 RadioButtonGroups having 2 Radio Buttons each called 'Yes' and 'No'. Default is 'No' 2) It has a Submit button. 3) After I click Submit, these 2 radiogroups selected radiobutton text should be displayed on second activity

I am trying to send values from 2 RadioGroup using Bundle object. But it shows the value of the 2nd radiogroup selected for both.

E.g. Radiogroup 1: Select 'Yes' RadioButton RadioGroup 2: Select 'No' RadioButton

On Second Activity: Value retreived for RadioGroup1 : No Value retreived for RadioGroup2 : No

So i tried swapping the code order. its observed that whicheveer radiobutton value code i write last, that value is passed to second activity for both radiogroup. Below is the code:

MainActivity.java

    Bundle dataBundle=new Bundle();

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                dataBundle.putString(ConfirmActivity.VARIABLE1, "Yes");                 
                dataBundle.putString(ConfirmActivity.VARIABLE2, "No");                                      
                i.putExtras(dataBundle);



ConfirmActivity:

public static final String VARIABLE1 = "No";
public static final String VARIABLE2 = "No";

String var1 = extras.getString(VARIABLE1);
String var2 = extras.getString(VARIABLE2);



 xml file:

<RadioGroup
     android:id="@+id/radio_group1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/upasana_no"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yes"
        android:textColor="@android:color/white" />

    <RadioButton
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:textColor="@android:color/white" />
</RadioGroup>



<RadioGroup
    android:id="@+id/radio_group2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/cd_no"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yes"
        android:textColor="@android:color/white" />

    <RadioButton
         android:id="@+id/btn4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="No"
         android:textColor="@android:color/white" />
</RadioGroup>

Solution

  • You have the same value in ConfirmActivity.VARIABLE1 and ConfirmActivity.VARIABLE2 as a result of which the value is being overwritten in dataBundle.

    Use separate values in ConfirmActivity.VARIABLE1 and ConfirmActivity.VARIABLE2

    for eg.

    public static final String VARIABLE1 = "value1";
    public static final String VARIABLE2 = "value2";