1
 public class Expandablelistview extends MainScreen {
    public Expandablelistview() {
    // A separator field between each type of control\
    // setTitle("Tree Field Demo");
    String parentfield1 = new String("Demo1");
    String parentfield2 = new String("Demo2");
    String childfield1 = new String("Demo3");
    String childfield2 = new String("Demo4");
    String parentfield3 = new String("Demo5");
    String parentfield4 = new String("Demo6");
    String childfield3 = new String("Demo7");
    String childfield4 = new String("Demo8");
    String childfield5 = new String("Demo9");
    String childfield6 = new String("Demo10");
    String parentfield5 = new String("Demo11");
    String childfield7 = new String("Demo12");
    String childfield8 = new String("Demo13");

    TreeCallback myCallback = new TreeCallback();
    final TreeField myTree = new TreeField(myCallback, Field.FOCUSABLE);
    myTree.setDefaultExpanded(false);

    int node12 = myTree.addChildNode(0, parentfield5);
    int node13 = myTree.addChildNode(node12, childfield7);
    int node14 = myTree.addChildNode(node12, childfield8);
    // int node7 = myTree.addChildNode(0, parentfield5);
    int node6 = myTree.addChildNode(0, parentfield4);
    int node11 = myTree.addChildNode(node6, childfield6);
    int node10 = myTree.addChildNode(node6, childfield5);
    int node8 = myTree.addChildNode(node6, childfield3);
    int node9 = myTree.addChildNode(node6, childfield4);
    int node5 = myTree.addChildNode(0, parentfield3);
    int node2 = myTree.addChildNode(0, parentfield2);
    int node3 = myTree.addChildNode(node2, childfield1);
    int node4 = myTree.addChildNode(node2, childfield2);
    int node1 = myTree.addChildNode(0, parentfield1);

    add(myTree);
    // myTree.setChangeListener(new myTreeChangeListener());

    // HERE I TRIED FOR ITEM CLICK


    FieldChangeListener fdbtncalculate = new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            // TODO Auto-generated method stub
            int a = myTree.getNodeCount();
            System.out.print("mytree" + a);

            if (a == 0) {
                Dialog.alert("data");
            } else if (a == 1) {
                Dialog.alert("data");
            }


        }
    };
    myTree.setChangeListener(fdbtncalculate);

}

private class TreeCallback implements TreeFieldCallback {
    public void drawTreeItem(TreeField _tree, Graphics g, int node, int y,
            int width, int indent) {
        String text = (String) _tree.getCookie(node);
                    g.drawText(text, indent, y);

    }

}

}

我想知道我做错了什么?我想在单击树视图的父项和子项时打开我的类文件,因为我使用了字段侦听器

4

1 回答 1

1

而不是使用 a FieldChangeListener,试试这个代码,它会覆盖 navigationClick()

  TreeCallback myCallback = new TreeCallback();
  TreeField myTree = new TreeField(myCallback, Field.FOCUSABLE) {
     protected boolean navigationClick(int status, int time) {
        // We'll only override unvarnished navigation click behavior
        if ((status & KeypadListener.STATUS_ALT) == 0 &&
              (status & KeypadListener.STATUS_SHIFT) == 0)
        {
           final int node = getCurrentNode();
           if (getFirstChild(node) == -1) {               
              // Click is on a leaf node.
              Dialog.alert("clicked leaf node " + getCookie(node));
              return true;
           } else {
              // Node is a parent node
              setExpanded(node, !getExpanded(node));
              Dialog.alert("clicked parent node " + getCookie(node));
              return true;
           }
        }
        return super.navigationClick(status, time);
     }
  };

我不确定“打开新的类文件”是什么意思,但是在用户单击树的一部分后,无论您想做什么,都可以在Dialog.alert()上面的代码中执行。

于 2013-09-24T08:14:37.567 回答