Reputation: 688
Some background: I am having an issue with one part of my program where I am building a 'edit database' view and then removing the rows again. The fields which the 'edit database' view will show are different each time it is shown. The edit database view is a tablelayout, and each row in the database is a row in the table layout. when the user exits the edit view, the view is supposed to be disposed or wiped of rows for the next time.
My issue, is no matter what I try, I can't get the rows in the tablelayout to delete. Here are the relevent pieces of code:
Get a handle on the tablelayout in the xml file
setContentView(R.layout.database_entry);
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
the layout is passed to another class, where they are added to the layout like this:
//layout is the tablelayout from earler, passed into this function
TableRow row = new TableRow(contex);
row .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//stuff added to the row....
//...
//...
layout.addView(row);
When I attempt to wipe the Tablelayout, I run this code
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
editorLayout.removeAllViews();
Whatever I do, the next time I hit the button to edit the code, it will show the previous rows, as well as the new rows added. They are not removed from the table. I get no error messages or any useful debugging information, it just isn't functioning.
I have struggled with this for a while. Tried dynamically creating the tablelayout(no xml file), tried deleting each row (or just ONE row) with it's index. I have used the Hierarchy Viewer to check to make sure everything is a child of that tablelayout (it is). If anyone has any insight, it would be much appreciated.
Upvotes: 1
Views: 1794
Reputation: 688
I was able to figure out the issue. First of all, the comment above was a little bit wrong, the mChildren array did not contain any pointers to the views I had deleted. They were nulled out, just as they should have been.
The reason why none of my rows were deleted is because right after I called removeAllViews()
I switched content views setContentView(R.layout.main);
. when I commented this line out, the remove children code worked as expected. I assume some android back-end optimization code decided that the removeAllViews()
didn't need to be run, or updated somewhere in memory. And all the changes made while inside this function didn't apply.
I will be putting all of this code in a new activity (where it really belonged in the beginning). But I wanted to provide an explanation in case someone else is having the same issue.
Upvotes: 2
Reputation: 564
After removing the row try setting the visibility to GONE and back to Visible again, This should fix the remove row glitch.
Upvotes: 1