本文介绍了黑莓 - TreeField与CheckBoxField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以添加到:
Same trick as with ListBox CheckBoxes:
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();
}
});
}
}
使用示例:
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);
}
}
这篇关于黑莓 - TreeField与CheckBoxField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!