Java 以编程方式从TableLayout中删除所有行
我使用以下代码从我的tablelayout中删除行; System.out.printlnvi;给我4个唯一的行,这是正确的,但为什么我的循环不是一次删除所有行。我需要点击3次 此代码放在myOnclickHandler中Java 以编程方式从TableLayout中删除所有行,java,android,tablelayout,Java,Android,Tablelayout,我使用以下代码从我的tablelayout中删除行; System.out.printlnvi;给我4个唯一的行,这是正确的,但为什么我的循环不是一次删除所有行。我需要点击3次 此代码放在myOnclickHandler中 public void onClick(View v) { TableLayout container = (TableLayout) v.getParent().getParent(); int childcount = container.getC
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);
}
}
试试看:
row = (TableRow)findViewById(R.id.row);
table.removeView(row);
使用此选项仅保留标题行
if (table.getRootView() != null) {
int i = 1;
while (table.getChildCount() != 1) {
table.removeViewAt(i);
}
}
是否检查了childcount值?请改为尝试container.removeAllViews。是,正如我所说,我得到了4个唯一的行作为结果。这是正在工作的container.removeAllViews;但我无法理解,如果container.removeAllViews删除了所有子行,并且在删除后添加了新的行,则会收到错误消息java.lang.IllegalStateException:指定的子行已经有父行。必须首先调用子级的父级上的removeView。