Android:你必须先在孩子的父母2上调用removeView()

时间:2014-04-22 14:30:01

标签: android parent-child

我在Android创建一个动态添加行的表时遇到问题。错误消息是:

  

指定的孩子已经有父母。你必须调用removeView()   在孩子的父母第一次

但为什么?

    void setCalendario(List<ArrayList<String>> l){
    TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
    TableRow tr = new TableRow(this);
    tr.removeAllViews();
    tr.setPadding(0,10,0,10);
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    TextView tv_item1 = new TextView(this);
    TextView tv_item2 = new TextView(this);
    TextView tv_item3 = new TextView(this);
    for (ArrayList<String> al : l){
        int i = 0;
        for(String s : al){
            if (i == 0){
                i++;
                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1){
                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2){
                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

    }

xml代码:

   <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"
    tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >

    <TableLayout
    android:id="@+id/list_tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:stretchColumns="0,1,2,3" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>


</TableRow>
</TableLayout>

</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

所以,你曾经创建过tv_item1,tv_item2和tv_item3。然后在循环中为所有ArrayList添加此视图

tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);

在第二次迭代中,您已经将tv_item1添加到tr。你想再做一次。我想你只需将这些行转移到循环:

TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);

答案 1 :(得分:1)

您正在使用for循环将TextView的相同引用添加到TableRow。因此,在循环的下一次迭代中,相同的对象再次添加到TableRow(或TableLayout)!那时他们已经有了父母。

尝试初始化(外部)for循环中的TableRowTextView个对象

编辑:修改了您的代码。

void setCalendario(List<ArrayList<String>> l) {
    // Here we initialize the objects we re-initialize every iteration of the loop
    TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
    for (ArrayList<String> al : l) {
        TableRow tr = new TableRow(this);
    // I can't believe a freshly initialized TableRow object has views attached...
        tr.removeAllViews();
        tr.setPadding(0,10,0,10);
    // Not sure why these layout params are needed already, as they are specified
    // when adding this TableRow to the TableLayout object as well.
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        TextView tv_item1 = new TextView(this);
        TextView tv_item2 = new TextView(this);
        TextView tv_item3 = new TextView(this);
        int i = 0;
        for(String s : al) {
            if (i == 0) {
                i++;
                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1) {
                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2) {
                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }
    } 

答案 2 :(得分:0)

因为你试图多次放置相同的textView。你只能使用它一次,所以你必须一次又一次地实例化它:

// you remove the definition of the text views here and you put it inside the loop
    for (ArrayList<String> al : l){
        int i = 0;
        for(String s : al){
 TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
            if (i == 0){
                i++;

                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1){

                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2){

                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

这不是最好的方法,你可能应该创建一个方法,让你的&#34; i&#34;作为参数并返回TextView(带重心......和你的员工)。