0

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>

3 Answers3

0

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";
Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

Try to replace this code :

public static final String VARIABLE1 = "No";

With this :

public static final String VARIABLE1 = "Yes";

Note : In your code you have override value of key "No" like first set "Yes" and then "No" so finally your both VARIABLE1 and VARIABLE2 have same value which is "No".

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

While passing bundle, your both key names are same i.e. NO. So it is replacing the previous value. Always make sure to have different key names. Otherwise it will override.

Try renaming

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

to

public static final String VARIABLE1 = "R1";
public static final String VARIABLE2 = "R2";
Aniruddha
  • 4,477
  • 2
  • 21
  • 39