【问题标题】:Remove all Rows Programmatically from TableLayout以编程方式从 TableLayout 中删除所有行
【发布时间】:2017-09-08 20:36:01
【问题描述】:

我使用以下代码从我的表格布局中删除行;
System.out.println(vi);给了我 4 个独特的行,这是正确的,但为什么我的循环没有一次删除所有行。我需要点击 3 次。

这段代码放在myOnclickHandler中;

   public void onClick(View v) { 

   TableLayout container = (TableLayout) v.getParent().getParent();

   int childcount = container.getChildCount();
   View vi;

   for (int i = 0; i < childcount; i++) {

           vi = container.getChildAt(i); 
           container.removeView(vi);


          System.out.println(vi);
   }


 }

【问题讨论】:

  • 你检查childcount值了吗?
  • 改用 container.removeAllViews()。
  • 是的,所以我说我得到了 4 个唯一的行作为结果
  • 是的,这是工作 container.removeAllViews();但我无法理解,如果 container.removeAllViews() 删除了所有子行并且在删除后我添加了新的行,我收到错误消息 java.lang.IllegalStateException: The specified child has a parent。您必须先在孩子的父母上调用 removeView()。

标签: java android tablelayout


【解决方案1】:

试试看:

   row = (TableRow)findViewById(R.id.row);
   table.removeView(row);

【讨论】:

    【解决方案2】:

    使用它只保留标题行。

    if (table.getRootView() != null) {
        int i = 1;
        while (table.getChildCount() != 1) {
            table.removeViewAt(i);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2010-12-04
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      相关资源
      最近更新 更多