Android的:你必须调用removeView()孩子的父母前2 [英] Android: You must call removeView() on the child's parent first 2

查看:128
本文介绍了Android的:你必须调用removeView()孩子的父母前2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安卓创建一个表动态地添加行的问题。错误消息是:

  

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

但是,为什么?

 无效setCalendario(名单< ArrayList的<字符串>> L){
    TableLayout表=(TableLayout)findViewById(R.id.list_tableLayout1);
    的TableRow TR =新的TableRow(本);
    tr.removeAllViews();
    tr.setPadding(0,10,0,10);
    tr.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    TextView的tv_item1 =新的TextView(本);
    TextView的tv_item2 =新的TextView(本);
    TextView的tv_item3 =新的TextView(本);
    对于(ArrayList的<字符串>人:L){
        INT I = 0;
        对于(字符串S:人){
            如果(我== 0){
                我++;
                tv_item1.setText(多个);
                tv_item1.setGravity(Gravity.CENTER);
            }
            如果(ⅰ== 1){
                tv_item2.setText(多个);
                tv_item2.setGravity(Gravity.CENTER);
                我++;
            }
            如果(ⅰ== 2){
                tv_item3.setText(多个);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(TR,新TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
            }
        }
    }

    }
 

在XML code:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.MSca.gorhinos.Calendario $ PlaceholderFragment>

    < TableLayout
    机器人:ID =@ + ID / list_tableLayout1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =10dp
    机器人:stretchColumns =0,1,2,3>

<的TableRow
    机器人:ID =@ + ID / tableRow1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>

<的TextView
                机器人:ID =@ + ID / textView1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:重力=中心
                机器人:文字颜色=#000000/>

<的TextView
                机器人:ID =@ + ID / textView2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:重力=中心
                机器人:文字颜色=#000000/>

<的TextView
                机器人:ID =@ + ID / textView3
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:重力=中心
                机器人:文字颜色=#000000/>


< /的TableRow>
< / TableLayout>

< / RelativeLayout的>
 

解决方案

所以,你一旦创建tv_item1,tv_item2和tv_item3。然后,在周期为您添加此观点的所有的ArrayList

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

在第二个迭代您已经tv_item1添加到TR。而你要再做一次。我想你需要的只是这个线转移到循环:

 的TableRow TR =新的TableRow(本);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView的tv_item1 =新的TextView(本);
TextView的tv_item2 =新的TextView(本);
TextView的tv_item3 =新的TextView(本);
 

I have a problem in Android creating a table adding rows dynamically. The error message is:

The specified child already has a parent. You must call removeView() on the child's parent first

But why?

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

    }

the xml 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"
    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>

解决方案

So, you once create tv_item1, tv_item2 and tv_item3. Then in cycle for all ArrayList you adding this views

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

At the second iteration you already add tv_item1 to tr. And you want to do it again. I guess you need just transfer this lines to cycle:

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

这篇关于Android的:你必须调用removeView()孩子的父母前2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆