开发者

Reusing a TableRow

In order to save time when a TableLayout is populated (with repetitive data), I want to reuse TableRows. However when I try to reuse them, I get the following e开发者_如何学编程rror:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I am storing the TableRows in an ArrayList. I then store the ArrayList in a Hashtable. This is how I am populating the ArrayList and 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);             
}

When I try to add the TableRow to the tableLayout, I get the error:

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

Thanks for the help.


On the second piece of code, try to clean the tableView before adding the Views again. Put something like this before the for block: tableView.removeAllViews();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜