views:

161

answers:

1

if i click on any checkbox all previous checkboxes must get checked "my logic works" if i uncheck a checkbox then all checkboxes after it must get unchecked "how to do that" MyLogic works for storm but not for other models what to do

//well what i want to do is i have  5 checkboxes
class myscreen
{
 chk_service = new CheckboxField[5];                      
 chk_service[0]= new CheckboxField("1",true)
 chk_service[1]= new CheckboxField("2",false)
 chk_service[2]= new CheckboxField("3",false)
 chk_service[3]= new CheckboxField("4",false)
 chk_service[4]= new CheckboxField("5",false)

 CheckboxFieldChangeListener obj = 
    new CheckboxFieldChangeListener(chk_service);
    chk_service[0].setChangeListener(obj);
    chk_service[1].setChangeListener(obj);
    chk_service[2].setChangeListener(obj);
    chk_service[3].setChangeListener(obj);
    chk_service[4].setChangeListener(obj);


  hm4 = new HorizontalFieldManager();
  hm4.add(chk_service[0]);
  hm4.add(chk_service[1]);
  hm4.add(chk_service[2]);
  hm4.add(chk_service[3]);
  hm4.add(chk_service[4]);

add(hm4);
}


    public CheckboxFieldChangeListener (CheckboxField[] arrFields)
    {
        m_arrFields = arrFields;

    }

    public void fieldChanged(Field field, int context)
    {
        if(true == ((CheckboxField) field).getChecked())
        {
            for(int i = 0; i < m_arrFields.length; i++)
            {
                if(m_arrFields[i]==field)
                {
                    //a[j]=i;
                    j++;
                    break;
                }
                else
                {
                CheckboxField oField = m_arrFields[i];
                oField.setChecked(true);
                }
            }           
        }
        a[k] = j;
        if(false == ((CheckboxField) field).getChecked())
        {
            for(int i =field.getIndex(); i < m_arrFields.length; i++)
            {
                if(m_arrFields[i]==field)
                {
                    //a[j]=i;
                    j++;
                    break;
                }
                else
                {
                CheckboxField oField = m_arrFields[i];
                oField.setChecked(false);
                }
            }           
        }


    }  

}
A: 

try this code, work on tour, storm, bold

alt text

class MyScreen extends MainScreen {
    int mCheckBoxesCount = 5;
    Vector mCheckBoxes = new Vector();
    CheckBoxListener mCBListener = new CheckBoxListener();    
    public MyScreen() {
        for (int i = 0; i < mCheckBoxesCount; i++) {
            CheckboxField cb = new CheckboxField(String.valueOf(i), false);
            cb.setChangeListener(mCBListener);
            add(cb);
            mCheckBoxes.addElement(cb);
        }
    }    
    class CheckBoxListener implements FieldChangeListener {
        boolean mProgrammatic = false;
        public void fieldChanged(Field field, int context) {
            if (!mProgrammatic) {
                mProgrammatic = true;
                CheckboxField cbField = (CheckboxField) field;
                int index = mCheckBoxes.indexOf(cbField);
                if (cbField.getChecked()) {
                    for (int i = 0; i < index; i++)
                        ((CheckboxField) mCheckBoxes.elementAt(i))
                                .setChecked(true);
                } else {
                    for (int i = index; i < mCheckBoxesCount; i++)
                        ((CheckboxField) mCheckBoxes.elementAt(i))
                                .setChecked(false);
                }
                mProgrammatic = false;
            }
        }
    }
}
Max Gontar