Ошибка в добавлении радиокнопок в радиогруппу в tableLayout в Android программно / динамически? - PullRequest
2 голосов
/ 23 марта 2012
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. где я застрял. Это дает мне эту ошибку

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 Ответ

1 голос
/ 24 марта 2012

Это происходит потому, что вы добавляете rb к двум GroupView (rg и newRow).RadioGroup является GroupView само по себе и не может добавить к нему соперников так же, как и другие GroupView.

Я думаю, что вам придется обрабатывать один выбор самостоятельно.Возможно, подключив того же слушателя выбора и сохранив копию выбранного переключателя, чтобы вы могли отменить выбор, когда выбран другой.С другой стороны, если вы хотите иметь множественный выбор, вы можете сохранить List с экземплярами RadioButton, а затем при необходимости проверить, какие из них выбраны, просто запустите их.

Надеюсь, это поможет.

...