1

一些背景知识:我的程序的一部分存在问题,我正在构建“编辑数据库”视图,然后再次删除行。每次显示时,“编辑数据库”视图将显示的字段都不同。编辑数据库视图是一个表格布局,数据库中的每一行都是表格布局中的一行。当用户退出编辑视图时,该视图应该在下一次被释放或擦除行。

我的问题是,无论我尝试什么,我都无法删除 tablelayout 中的行。以下是相关的代码片段:

获取xml文件中tablelayout的句柄

setContentView(R.layout.database_entry);
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);

布局被传递给另一个类,在那里它们被添加到布局中,如下所示:

//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);

当我尝试擦除 Tablelayout 时,我运行此代码

TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
editorLayout.removeAllViews();

无论我做什么,下次我点击按钮编辑代码时,它都会显示以前的行,以及添加的新行。它们不会从表中删除。我没有收到任何错误消息或任何有用的调试信息,它只是无法正常工作。

我已经为此苦苦挣扎了一段时间。尝试动态创建表格布局(无 xml 文件),尝试使用其索引删除每一行(或仅一行)。我已经使用 Hierarchy Viewer 来检查以确保所有内容都是该 tablelayout 的子项(它是)。如果有人有任何见解,将不胜感激。

4

2 回答 2

2

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.

于 2012-05-13T19:36:45.250 回答
1

删除行后尝试将可见性设置为 GONE 并再次返回可见,这应该可以修复删除行故障。

于 2012-04-09T20:03:06.170 回答