Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all rows from a TableLayout?

I know that this has already been asked before, but none of the other responses have helped me, so I'll ask myself...

I am trying to delete all my existing rows from a TableLayout because I want the user to be able to update the table dynamically. Other suggestions have recommended using removeAllViews(), which is supposed to remove all child views, however this deletes rows from my other tables in the same LinearLayout (I have a linear layout with multiple tables).

Any suggestions?

like image 394
Fran Fitzpatrick Avatar asked Sep 04 '11 23:09

Fran Fitzpatrick


2 Answers

It sounds like you might be calling removeAllViews() on the whole LinearLayout and not the particular TableLayout you want to clear. Double check you have some thing like:

myLinearLayout.someTableView.removeAllViews()

like image 196
SK9 Avatar answered Oct 13 '22 11:10

SK9


You need to call removeAllViews() on each TableRow:

int count = table.getChildCount();
for (int i = 0; i < count; i++) {
    View child = table.getChildAt(i);
    if (child instanceof TableRow) ((ViewGroup) child).removeAllViews();
}
like image 14
Romain Guy Avatar answered Oct 13 '22 10:10

Romain Guy