I have an array of CheckboxField[] elements that I need to dynamically initialize. My sample code is -
class abc extends MainScreen implements FieldChangeListener {
CheckboxField[] boxField;
abc() {
.
.
.
boxField = new CheckboxField[length];
VerticalFieldManager vfm = new VerticalFieldManager();
for(int i=0; i<length; i++) {
boxField[i] = new CheckboxField(var[i], false);
boxField[i].setChangeListener(this);
vfm.add(boxField[i]);
}
add(vfm);
}
public void fieldChanged(Field field, int context) {
// The idea is to disable all the other checkboxes when one
// is clicked.
boxField[0].setChecked(false); // Gives stackoverflow error on JVM.
}
}
Any help?
Edit: The problem only seems to be with .setChecked(boolean) I've tried chkboxField[0].setFont(), chkboxField.getChecked(), both of th开发者_如何学JAVAem seem to work.
So, what's apparently happening is boxField[i].setChecked(false)
calls the FieldChangeListener again, and this loops infinitely till the stack blows.
I was told to use
if(context != FieldChangeListener.PROGRAMMATIC) {
boxField[i].setChecked(false);
}
Based on your comment in the FieldChanged method, it sounds like you have mutually exclusive checkboxes (that is, you have a group of checkboxes and when any one is checked, all the rest should be unchecked).
If so, you may want to consider using the RadioButtonField instead. You can stick your radio buttons into a RadioButtonGroup and then the BlackBerry will take care of unchecking for you.
精彩评论