android - 在 android programaticall/dynamically 中将单选按钮添加到 tableLayout 中的单选组时出错?

标签 android radio-button removechild radio-group android-tablelayout

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

我的问题是我想在我的结果的每一行前面动态添加一个单选按钮,上面的代码没有问题,但是因为要选择其中一行,我需要获得一个单选组中的所有单选按钮。这是我被困在哪里。它给了我这个错误

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.

我不知道上面是什么,上面代码中的这一行给我错误 ->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>

最佳答案

发生这种情况是因为您将 rb 添加到两个不同的 GroupView(rgnewRow)。 RadioGroup 本身就是一个 GroupView,不能像其他 GroupView 一样向它添加 View 。

我认为您将不得不自己处理单个选择。可能通过附加相同的选择监听器并保留所选单选按钮的副本,以便您可以在选择另一个单选按钮时取消选择它。另一方面,如果你想有多个选择,你可以保留一个 ListRadioButton 实例,然后在需要检查哪些被选中时运行它。

希望对您有所帮助。

关于android - 在 android programaticall/dynamically 中将单选按钮添加到 tableLayout 中的单选组时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844001/

相关文章:

python - 在views.py django中检索多个单选按钮和表单字段的值

python xml删除孙子或孙子

java - 如何使用 java 代码在 android 中锁定文件?

java - 简单计算器应用程序中的资源$NotFoundException

Android Studio 卡在安装 APK,adb 有时无法安装 APK,并且 Android Studio 以某种方式干扰了 adb 的安装命令

javascript - jQuery 设置按钮选择的事件类

javascript - 禁用单选按钮的CSS

javascript - 在纯 JavaScript 中一一删除 childElements

javascript - Internet Explorer 和 removeChild()

java - 将jetty服务器嵌入到android应用程序中