Question

RadioGroup rg = new RadioGroup(this);

            for (int i = 0; i < e.length(); i++) 
            {
                JSONObject arrayElement = e.getJSONObject(i);   
                TableRow newRow = new TableRow(this);
                                // add views to the row
                TextView tv1 = new TextView(this);
                tv1.setText(arrayElement.getString("name"));
                newRow.addView(tv1); // you would actually want to set properties on this before adding it
                arr[j++]=arrayElement.getString("name");

               TextView tv2 = new TextView(this);
                tv2.setText(arrayElement.getString("specialization"));
                newRow.addView(tv2);
               arr[j++]=arrayElement.getString("specialization");

               TextView tv3 = new TextView(this);
                tv3.setText(arrayElement.getString("emailid"));
                newRow.addView(tv3);
               arr[j++]=arrayElement.getString("emailid");

               TextView tv4 = new TextView(this);
                tv4.setText(arrayElement.getString("day"));
                newRow.addView(tv4);
               arr[j++]=arrayElement.getString("day");

                TextView tv5 = new TextView(this);
                String t1=arrayElement.getString("tf");
                t1=t1.substring(11,16);
                tv5.setText(t1);
                newRow.addView(tv5);
               arr[j++]=arrayElement.getString("tf");

                TextView tv6 = new TextView(this);
                String t2=arrayElement.getString("tt");
                t2=t2.substring(11,16);
                tv6.setText(t2);
                newRow.addView(tv6);
               arr[j++]=arrayElement.getString("tt");

               TextView tv7 = new TextView(this);
                tv7.setText(arrayElement.getString("place"));
                newRow.addView(tv7);
               arr[j++]=arrayElement.getString("place");
            // add the row to the table layout

              RadioButton rb = new RadioButton(this);
               rg.addView(rb);
               newRow.addView(rb);
                tl.addView(newRow);

            } 
          // tl.addView(rg);
        }
    } 
    catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    catch (JSONException e) 
    {
        e.printStackTrace();
   }

My problem is that i want to dynamically add a radiobutton infront of each row of my result, which the above code does fine , But since to select one of the row i need to get all the radiobuttons in a radioGroup.This is where i am stuck.it is giving me this error

03-23 22:43:37.807: E/AndroidRuntime(551): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

i have no idea what the above is , this line in the above code is giving me the error ->rg.addView(rb);

xml file

 <ScrollView android:id="@+id/ScrollView1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="@+id/HorizontalScrollView01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:scrollbars="horizontal|vertical"
                      >
    <TableLayout
        android:id="@+id/tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.73"  
         >

        <TableRow
            android:id="@+id/Heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            >


            <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textSize="16sp" 
            android:background="@layout/shape"/>
            <TextView
            android:id="@+id/Specialization"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Specialization"
            android:textSize="16sp" 
            android:background="@layout/shape"/>
            <TextView
            android:id="@+id/Emailid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Emailid"
            android:textSize="16sp" 
            android:background="@layout/shape"/>
            <TextView
            android:id="@+id/Day"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Day"
            android:textSize="16sp"
            android:background="@layout/shape" />
            <TextView
            android:id="@+id/tf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="From"
            android:textSize="16sp" 
            android:background="@layout/shape"/>
            <TextView
            android:id="@+id/tt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="To      "
            android:textSize="16sp"
            android:background="@layout/shape"/>
            <TextView
            android:id="@+id/pl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Place"
            android:textSize="16sp" 
            android:background="@layout/shape"/>


        </TableRow>


    </TableLayout>

    </HorizontalScrollView>
    </ScrollView>
Was it helpful?

Solution

This is happening because you are adding rb to two different GroupViews (rg and newRow). RadioGroup is a GroupView on it's own and can't add vies to it as wel as to other GroupView.

I think that you are going to have to handle the single selection yourself. Possibly by attaching the same selection listener and keeping a copy of the selected radio button so you can deselect it when another one is selected. On the other hand if you want to have multiple selection you can keep a List with the RadioButton instances and then when needing to check which ones are selected just run though it.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top