如何清除TableLayout中的所有行?

时间:2011-09-04 23:12:17

标签: android android-layout tableview

我知道之前已经提出这个问题了,但是没有其他回复对我有所帮助,所以我会问自己......

我正在尝试从TableLayout删除所有现有行,因为我希望用户能够动态更新表。其他建议建议使用removeAllViews(),它应该删除所有子视图,但是这会从同一LinearLayout中的其他表中删除行(我有一个包含多个表的线性布局)。

有什么建议吗?

4 个答案:

答案 0 :(得分:51)

听起来您可能会在整个removeAllViews()上调用LinearLayout而不是您要清除的特定TableLayout。仔细检查你有一些事情:

myLinearLayout.someTableView.removeAllViews()

答案 1 :(得分:11)

您需要在每个TableRow上调用removeAllViews():

int count = table.getChildCount();
for (int i = 0; i < count; i++) {
    View child = table.getChildAt(i);
    if (child instanceof TableRow) ((ViewGroup) child).removeAllViews();
}

答案 2 :(得分:0)

您可以简单地使用yourTableView.removeViews(startIndex, count) startIndex 是您要开始删除的整数值。如果您有标题并且不想删除它,请从位置1开始,否则从0开始。 计数 是要删除的行数。

答案 3 :(得分:0)

如果要删除除第一行(通常用作标题)以外的所有行,可以使用:

tableLayout.removeViews(1, Math.max(0, tableLayout.getChildCount() - 1));