为了节省填充TableLayout的时间(使用重复数据),我想重用TableRows。但是当我尝试重用它们时,我收到以下错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我将TableRows存储在ArrayList中。然后我将ArrayList存储在Hashtable中。这就是我填充ArrayList和Hashtable的方法:
ArrayList<TableRow> tableRowAl = AcmSinglton.rowHash.get(this.cfr);
// if ArrayList is null, populate the ArrayList with the TableRows
if (tableRowAl == null){
NodeList nodeList = (NodeList) xpath.evaluate(this.xpath, xmlSource, XPathConstants.NODESET);
for(int i=0; i<nodeList.getLength(); i++){
TableRow tr = new TableRow(this.activity);
tr.removeAllViews();
tr.setId(i);
TextView tv = new TextView(this.activity);
Element chapElement = (Element) nodeList.item(i);
tv.setText(chapElement.getAttribute(this.attribute));
tr.addView(tv);
rowsAl.add(tr);
}
// put the ArrayList<TableRow> into the hashtable for reuse
AcmSinglton.rowHash.put(this.cfr, rowsAl);
}else{
// Resuse the TableRows
this.rowsAl.addAll(tableRowAl);
}
当我尝试将TableRow添加到tableLayout时,我收到错误:
ArrayList<TableRow> al = new ArrayList<TableRow>();
// get the Arraylist
al = xmlloaded.getRowsAl();
for (int x=0; x < al.size(); x++){
TableRow tableRow = new TableRow(this);
tableRow = al.get(x);
View child = tableRow.getChildAt(0);
final TextView tx = (TextView)child;
// I get the error here when I try to add the TableRow
tableView.addView(tableRow);
}
感谢您的帮助。
答案 0 :(得分:0)
在第二段代码中,尝试在再次添加Views之前清理tableView。在之前添加类似的内容: tableView.removeAllViews();