BlackBerry - TreeField with CheckBoxField?

妖精的绣舞 提交于 2020-01-13 05:38:31

问题


Is it possible to add a CheckBoxField to a TreeField in BlackBerry ?

If yes, how do I do it?


回答1:


Same trick as with ListBox CheckBoxes:
alt text http://img441.imageshack.us/img441/5120/checkboxtree.jpg

class CBTreeField extends VerticalFieldManager implements TreeFieldCallback,
        DrawStyle {
    boolean[] mBooleanValues = new boolean[] {};
    String[] mStringValues = new String[] {};

    public boolean getNodeBooleanValue(int node) {
        return mBooleanValues[node];
    }

    public void setNodeBooleanValue(int node, boolean value) {
        mBooleanValues[node] = value;
    }

    TreeField mRootField = null;

    public CBTreeField(String rootString, boolean rootBoolean) {
        mRootField = new TreeField(this, TreeField.FOCUSABLE);
        add(mRootField);
        mStringValues = insertAt(mStringValues, 0, rootString);
        mBooleanValues = insertAt(mBooleanValues, 0, rootBoolean);
    }

    public int addSiblingNode(int previousSibling, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addSiblingNode(previousSibling, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    public int addChildNode(int parent, String stringValue,
            boolean booleanValue, Object cookie) {
        int index = mRootField.addChildNode(parent, cookie);
        mBooleanValues = insertAt(mBooleanValues, index, booleanValue);
        mStringValues = insertAt(mStringValues, index, stringValue);
        return index;
    }

    static boolean[] insertAt(boolean[] inArray, int index, boolean value) {
        int newLen = inArray.length + 1;
        boolean[] outArray = new boolean[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    static String[] insertAt(String[] inArray, int index, String value) {
        int newLen = inArray.length + 1;
        String[] outArray = new String[newLen];
        for (int i = 0, j = 0; i < newLen; i++, j++) {
            outArray[i] = (i != index) ? inArray[j] : value;
            if (i == index)
                j++;
        }
        return outArray;
    }

    public void drawTreeItem(TreeField treeField, Graphics g, int node, int y,
            int width, int indent) {
        String check = String
                .valueOf(mBooleanValues[node] ? 
                Characters.BALLOT_BOX_WITH_CHECK : Characters.BALLOT_BOX);
        g.drawText(check, indent, y, DrawStyle.LEFT);
        g.drawText(mStringValues[node], indent + 20, y, DrawStyle.RIGHT
                | ELLIPSIS);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Change value", 0, 0) {
            public void run() {
                int node = mRootField.getCurrentNode();
                mBooleanValues[node] = !mBooleanValues[node];
                invalidate();
            }
        });
    }
}

sample of use:

class Scr extends MainScreen {
    public Scr() {
        CBTreeField tree = new CBTreeField("root", false);
        add(tree);

        int ch01 = tree.addChildNode(0, "child 0-1", true, null);
        int ch02 = tree.addChildNode(0, "child 0-2", false, null);
        int ch03 = tree.addChildNode(0, "child 0-3", false, null);
        int ch011 = tree.addChildNode(ch01, "child 0-1-1", false, null);
        int ch012 = tree.addChildNode(ch01, "child 0-1-2", true, null);
        int ch031 = tree.addChildNode(ch03, "child 0-3-1", true, null);
    }
}


来源:https://stackoverflow.com/questions/1038048/blackberry-treefield-with-checkboxfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!