2011-09-04 93 views
13

我知道这已经被问过,但没有其他答案帮助过我,所以我会问自己...如何清除TableLayout中的所有行?

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

有什么建议吗?

回答

46

听起来好像你可能会打电话给removeAllViews()整个LinearLayout而不是你想要清除的特定TableLayout。仔细检查你有像一些事情:

myLinearLayout.someTableView.removeAllViews()

+0

不,我一定打电话给我TableLayout ... \t \t tl =(TableLayout)dialog.findViewById(R.id.afi_waist_table); \t \t tl.removeAllViews(); –

+0

这里是我的布局:http://imagebin.org/171056 –

+0

改变了我的代码,并能够让removeAllViews()工作。 –

10

你需要在每个调用的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(); 
} 
+0

你的建议将删除该行的所有子视图,但不会删除该行本身。表格布局将留空行。 – SK9

+0

我知道,这就是我认为的原始海报想要的。 –