2011-09-04 95 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

我知道,這就是我認爲的原始海報想要的。 –