views:

441

answers:

2

Hi,

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 them seem to work.

+2  A: 

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); 
}
Tejaswi Yerukalapudi
A: 

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.

Eric
I wanted to do radio buttons, but the UI seemed better to look at when using check boxes and I didn't want to create custom radio buttons since this seemed a much simpler alternative :S
Tejaswi Yerukalapudi