如何清除TableLayout中的所有行?

Fra*_*ick 16 android tableview android-layout

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

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

有什么建议?

SK9*_*SK9 51

听起来你可能会removeAllViews()在整体上呼唤,LinearLayout而不是TableLayout你想要清除的特殊情况.仔细检查你有一些事情:

myLinearLayout.someTableView.removeAllViews()


Rom*_*Guy 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();
}
Run Code Online (Sandbox Code Playgroud)