为了节省填充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存储在哈希表中。我就是这样填充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);
}谢谢你的帮助。
发布于 2011-08-14 20:34:21
在第二段代码中,尝试在再次添加视图之前清除tableView。将类似的内容放在for块之前: tableView.removeAllViews();
https://stackoverflow.com/questions/7057328
复制相似问题