Android: You must call removeView() on the child's parent first 2

前端 未结 3 1593
予麋鹿
予麋鹿 2021-01-25 06:00

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

The specified child already has a parent. You mu

3条回答
  •  無奈伤痛
    2021-01-25 06:39

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

提交回复
热议问题