首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重用TableRow

重用TableRow
EN

Stack Overflow用户
提问于 2011-08-14 14:17:54
回答 1查看 714关注 0票数 0

为了节省填充TableLayout (使用重复数据)时的时间,我想重用TableRows。然而,当我尝试重用它们时,我会得到以下错误:

代码语言:javascript
复制
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的:

代码语言:javascript
复制
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时,会得到以下错误:

代码语言:javascript
复制
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);     
}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2011-08-14 20:34:21

在第二段代码中,尝试在再次添加视图之前清除tableView。将类似的内容放在for块之前: tableView.removeAllViews();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7057328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档