Question

I need to traverse a Tree Field in Blackberry . I need to know how to check from the existing node whether it is a parent node or child node ? Since I need a different row color for Child rows and Parent rows

I have referred a previous question of Customising TreeField as seen in link below

Customising Blackberry Treefield

Was it helpful?

Solution

It looks like you're already using the TreeFieldCallback to customize painting of your tree field. You can detect parent vs. child nodes here in drawTreeItem().

However, we need to be clear about what you consider "parent" and "child", because technically, it's possible for a row to be both a parent and a child, if the tree has more than 2 levels (in addition to the root level).


If "Child" == "Leaf" Node

Use:

public void drawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent)  {

    boolean isChild = treeField.getFirstChild(node) == -1;
    if (isChild) {
        // draw child row color
    } else { 
        // draw parent row color
    }
} 

assuming that what you mean by "child" is a row that has no more children of its own (also called a "leaf" node).


If "Child" == "Non-Root Parent"

If you consider a node that has both a non-root parent of its own and a child of its own to be a "child" row, then use this logic:

public void drawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent)  {

    boolean isParent = treeField.getParent(node) == 0;
    if (isParent) {
        // draw parent row color
    } else { 
        // draw child row color
    }
}

this second implementation will only color rows that are directly under the root node as "parents".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top