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

相關問題