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

私の問題は、結果の各行の前にラジオボタンを動的に追加したいことです。これは上記のコードで問題ありませんが、行の 1 つを選択するには、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>
4

1 に答える 1

1

rbこれは、2 つの異なるGroupViews (rgおよび)に追加しているためですnewRowRadioGroupGroupViewそれ自体であり、他のものと同様にそれにバイを追加することはできませんGroupView

単一の選択を自分で処理する必要があると思います。おそらく、同じ選択リスナーをアタッチし、選択したラジオボタンのコピーを保持して、別のラジオボタンが選択されたときに選択を解除できるようにすることによります。一方、複数の選択が必要な場合は、インスタンスを保持し、選択されてListいるRadioButtonインスタンスを確認する必要がある場合は、それを実行するだけです。

それが役に立てば幸い。

于 2012-03-24T02:16:02.363 に答える