cancel
Showing results for 
Search instead for 
Did you mean: 

Unexpected behavior using addElement(int, object)

Former Member
0 Kudos

Hi,

My code produces unexpected behavior. When I execute the following code, the elements are added but other (existing) elements are duplicated.

When I replace "addElement(i, el)" with addElement(el), it works fine, except the tablerow is inserted at the end of the table, which is not the right location.

i=0;
IPrivateResultView.IEp_OpportunitiesNode node = wdContext.nodeEp_Opportunities();
while ( i < node.size() ) {
  total1 += <some value>;
  if (<start of new section>) {
    IPrivateResultView.IEp_OpportunitiesElement el = (IPrivateResultView.IEp_OpportunitiesElement)
      node.createElement(new Zopportunities());
    el.setAttributeValue("Exp_Revenue", new BigDecimal(total1));

    /* totals row is added here: */
    node.addElement(i, el);

    total1 = 0;
  }
  i++;
}

Thanks in advance,

Roelof

Message was edited by: Armin Reichert

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

If I understand your code correctly, you want to insert "total" rows at certain indices.

The problem with your code is that you are adding elements to the node that you are iterating.

You could iterate the node and build a second list "elementsWithTotals" where you either add the current element or a new "total" element.

After the loop, rebind the node to the new list.

Armin

Former Member
0 Kudos

see if this helps...

i=0;

IPrivateResultView.IEp_OpportunitiesNode node = wdContext.nodeEp_Opportunities();

while (i<node.size()) {

total1 += <some value>;

if (<start of new section>) {

IPrivateResultView.IEp_OpportunitiesElement el = (IPrivateResultView.IEp_OpportunitiesElement) node.createElement(new Zopportunities());

el.setAttributeValue("Exp_Revenue", new BigDecimal(total1));

// totals row is added here:

<b>node.addElement(i, el);</b>

<b>instead use</b>

<b>i++;</b>

<b>node.addElement(i, el);</b>

total1 = 0;

}

i++;

}