2016-09-16 41 views
1

我试着用以下方法如何获取表的索引值?

if(tbl.getParent() instanceof Body) 
{ 
    Body body = (Body) tbl.getParent(); 
    int tIndex = body.getContent().indexOf(tbl); 
    body.getContent().remove(tbl); 
} 

得到一个表的索引值,而另一种方法是使用contentAccessor

if(tbl.getParent() instanceof ContentAccessor) 
{ 
    ContentAccessor ca = (ContentAccessor) tbl.getParent(); 
    int tIndex = ca.getContent().indexOf(tbl); 
    ca.getContent().remove(tbl); 
} 

而不是让实际的索引值我越来越但是-1作为tIndex。它也不会从它的父项中删除tbl(ca.getContent()。remove(tbl);)。

是否有任何其他方式来获得Tbl的索引值?

回答

1
ContentAccessor ca = (ContentAccessor) tbl.getParent(); 
    int tIndex = getIndex(ca.getContent(), tbl); 
    if(tIndex != 98761){ 
    //do whatever you want to 
    } 

    private static int getIndex(List<Object> theList, Object bm) 
    { 
     for (Object ox : theList) 
     { 
     if (XmlUtils.unwrap(ox).equals(bm)) 
     { 
      int o = theList.indexOf(ox); 
      return o; 

     } 
     } 
    return 98761; 

    } 

在我的情况下,tIndex不会触及那么多的数字(98761)。如果您觉得不安全,只需增加返回值。