2

How can i get the data from the Table Row on the button Click Event from the selected Radio Button. How i can set the Radio Group in a Radio button also when i write rg.addView(rb); My Android Application is Closed Unfortunately.

My XML File code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dynamictablelayout.Round_Trip_Search_Result" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:id="@+id/tl"
    android:stretchColumns="*" >
</TableLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tl"
    android:id="@+id/ll"
    android:orientation="vertical" >
</LinearLayout>
<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ll"
    android:id="@+id/tl1"
    android:stretchColumns="*" >
</TableLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/ll1"
    android:orientation="vertical" >
</LinearLayout>

my activity file

TextView tv;
RadioGroup rg, rg1;
RadioButton rb;
TableRow tr;
int cnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_round__trip__search__result);

    String[] str = { "Airindia", "Ai-101", "3.00", "2000", "Go Air",
            "GA-101", "4.00", "2500" };

    String[] str1 = { "Air India", "Ai-100", "5.00", "2500",
            "Etihad Airways", "EI-200", "12.00", "3500" };

    TableLayout tl = (TableLayout) findViewById(R.id.tl);
    TableLayout tll = (TableLayout) findViewById(R.id.tl1);
    LinearLayout l1 = (LinearLayout) findViewById(R.id.ll);
    LinearLayout l11 = (LinearLayout) findViewById(R.id.ll1);

    int k = 0;
    int tr_id = 100;
    for (int i = 0; i < str.length; i += 4) {
        tr = new TableRow(this);
        tr.setId(tr_id);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 4; j++) {
            tv = new TextView(this);
            tv.setText(str[k]);
            tr.setId(tr_id);
            tr.addView(tv);
            k++;
        }
        rg = new RadioGroup(this);
        rb = new RadioButton(this);
        //rg.addView(rb);
        rb.setId(tr_id);
        tr.addView(rb);
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tr_id++;
    }
    TextView rtn = new TextView(this);
    rtn.setText("Flight from XXX to YYY");
    l1.addView(rtn);
    int h = 0;
    for (int i = 0; i < str1.length; i += 4) {
        tr = new TableRow(this);
        tr.setId(tr_id);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 4; j++) {
            tv = new TextView(this);
            tv.setText(str1[h]);
            tr.setId(tr_id);
            tr.addView(tv);
            h++;
        }
        rg1 = new RadioGroup(this);
        rb = new RadioButton(this);
        // rg1.addView(rb);
        rb.setId(tr_id);
        tr.addView(rb);
        tll.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tr_id++;
    }

    Button b = new Button(this);
    b.setText("book");
    l11.addView(b);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Click",
                    Toast.LENGTH_SHORT).show();
            int id = rb.getId();
            Toast.makeText(getApplicationContext(), "ID =>" + id,
                    Toast.LENGTH_SHORT).show();
        }
    });

LogCat Error Occurs when i remove the line rg.addView(rb);:

03-20 10:25:32.085: E/AndroidRuntime(6235):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
03-20 10:25:32.085: E/AndroidRuntime(6235):     at android.view.ViewGroup.addView(ViewGroup.java:3249)
03-20 10:25:32.085: E/AndroidRuntime(6235):     at android.view.ViewGroup.addView(ViewGroup.java:3194)
03-20 10:25:32.085: E/AndroidRuntime(6235):     at android.view.ViewGroup.addView(ViewGroup.java:3170)
03-20 10:25:32.085: E/AndroidRuntime(6235):     at com.example.dynamictablelayout.Round_Trip_Search_Result.onCreate(Round_Trip_Search_Result.java:61)
4

1 回答 1

0
rg1.addView(rb);
rb.setId(tr_id);
tr.addView(rb);

在上面的第一行和第三行中,您将父元素添加到同一元素“rb”,而不删除其父元素。一个孩子(rb)不能有两个父母。您必须首先在孩子的父母上调用 removeView()。

于 2015-03-20T11:11:48.800 回答