2
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(); 
    } 

我的问题是,我想动态地添加我结果的每一行的单选按钮面前,上面的代码没有问题,但自从选择其中一行,我需要获取radioGroup中的所有单选按钮。这是我在哪里stuck.it是给我这个错误在android programaticall/dynamic中向tableLayout中的radiogroup添加单选按钮时出错?

03-23 22:43:37.807:E/AndroidRuntime(551): java.lang.IllegalStateException:由此造成的指定子已经有一个 父母。您必须先调用子对象的父对象的removeView()。

我不知道上面是什么,上面代码中的这行代码给了我错误 - > rg.addView(rb);

xml文件

<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> 

回答

1

这是发生,因为您要添加rb到两个不同的GroupView S(rgnewRow)。 RadioGroup本身就是GroupView,无法为其他GroupView添加激活码。我认为你将不得不自己处理单个选择。可能通过附加相同的选择监听器并保留所选单选按钮的副本,以便在选择另一个单选按钮时取消选择它。另一方面,如果你想有多个选择,你可以保留一个ListRadioButton实例,然后当需要检查哪些被选中时,只需运行即可。

希望它有帮助。

+0

我很感谢你的回答,但没有任何方法可以将这些动态生成的单选按钮添加到radiogroup? – prabhjit 2012-03-25 11:40:15

+0

不是他们设计的。 'RadioGroup'是一个'ViewGroup',所以它是'TableRow',你不能有两个父母的View(按照设计)。 – pablisco 2012-03-26 09:16:55

相关问题