1

i am a basic learner in android. What i am trying to achieve from my code is that : i want to dynamically change the value of radio buttons of a radio group inside a for loop. Then, grab the text value so that i can compare, lets say if the value is "Take Out", i want the rb1 to be checked. Here is the radio group

<RadioGroup
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_alignBottom="@+id/add"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="20dp"
        android:id="@+id/rg1">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take Out"
            android:id="@+id/rb1"
            android:checked="true"
            android:onClick="selectSomething"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sit down"
            android:id="@+id/rb2"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delivery"
            android:id="@+id/rb3"
            android:checked="false" />
    </RadioGroup>

What i have tried so far:

int count = rg1.getChildCount();

        //Make checked if the value is equal
        for(int i = 0; i < count;i++){
            String myshit = "rb"+i;

            int id = getResources().getIdentifier(myshit, "id", getPackageName());
            RadioButton RadioValue = (RadioButton) findViewById(id);

            String _RadioValue = RadioValue.getText().toString();



            Log.d("abc",_RadioValue);

        }

For testing, i wanted to print out the values in Log but it does not compile and throws error. I am just half way thorugh in my code. Is there a better way to solve this problem ? Need your expertise. Thank You !

Wang'l Pakhrin
  • 858
  • 3
  • 15
  • 29

1 Answers1

1

You are doing it the wrong way.

All of your RadioButtons should point to a common function.
That is, all of them should have this property assigned:

android:onClick="selectSomething"

Inside that method, do something like:

public final void selectSomething(final View v)
{
    switch(v.getId())
    {
        case R.id.rb1:
        {
            String str = "Take out";
            System.out.println(str);
            break;
        }
        // all other cases
        // ...
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • hello @Der Golem, I think you did not get the picture quite well. Let's say that in 1st Screen i selected one radio box out of 3. Then send it's value to the 2nd Screen. Now, in second screen , i have the same layout as the 1st Screen. The second screen is like a confirmation screen about what i selected earlier in 1st Screen. So , as soon as the 2nd screen opens, what radio button i i selected should be checked in the 2nd screen. Hope you got what i am trying to do. I do not want it to be assigned in a click event – Wang'l Pakhrin Mar 22 '15 at 22:07
  • Simply send the RadioButton name as a string to the second Activity (or uses a shared variable). Then use reflection and set it as checked in the second Activity. Having 2 identical Activities doesn't seem a great design, though... – Phantômaxx Mar 23 '15 at 16:56
  • can you give me some idea on reflection?. Yes i know it does not make sense with the two activities, it's for learning purpose. Thank you. – Wang'l Pakhrin Mar 23 '15 at 19:42
  • I recently answered something similar: http://stackoverflow.com/a/29182221/2649012. This was for drawables. Just replace "drawable" with "id" and pass the name. The "name" is `rb1`,`rb2`, ... – Phantômaxx Mar 23 '15 at 19:43