treeblackberrytreefield

Using TreeField , expand or collapse a Tree in Blackberry?


I have drawn a tree structure using TreeField in Blackberry now . The root node consists of 15 nodes and every node has its corresponding child nodes and on running the application , all the node comes as expanded . I need only a particular node to be expanded and all the others as collapsed .

I have tried using the following method from apis .

myTree.setDefaultExpanded(false);

and

myTree.setExpanded(1, true);

but does not seem to have any effect so far on the tree . All nodes comes as expanded on launching the application .


Solution

  • If you look at the documentation for TreeField, you'll see that setDefaultExpanded() must be called before you add the nodes, not just before they are displayed:

    setDefaultExpanded

    public void setDefaultExpanded(boolean expanded)
    

          Sets the default expansion state for future node allocations.

    So, to modify the TreeField example, you'd do something like this:

       public TreeFieldDemoScreen()
       {             
          setTitle("Tree Field Demo");
    
          String fieldOne =  new String("Parent folder 1");
          String fieldTwo =  new String("Parent folder 2");
          String fieldThree =  new String("Sub-folder 1");
          String fieldFour =  new String("Sub-folder 2");
    
          TreeCallback myCallback = new TreeCallback();
          TreeField myTree = new TreeField(myCallback, Field.FOCUSABLE);
          myTree.setDefaultExpanded(false);
          
          int node1 = myTree.addChildNode(0, fieldOne);
          int node2 = myTree.addChildNode(0, fieldTwo);
          int node3 = myTree.addChildNode(node2, fieldThree);
          int node4 = myTree.addChildNode(node3, fieldFour);
    
          add(myTree);
       }