1
我正在实现一个简单的应用程序,在注册页面用户可以选择新闻类别。要求如下黑莓手机中的Stackoverflow例外CheckBoxField
- 所有的类别都是CheckBoxField的。用户必须至少选择一个类别。
- 选择所有CheckBox将允许选择所有/取消选择所有类别CheckBox。
- 如果用户手动选择所有复选框字段,则必须选择“全选”复选框。
方法:我在循环中创建了类别复选框。
for(int i=0;i<interests.length;i++){
allFields[i] = new ColorCheckBoxField(interests[i], false, checkBoxStyle | USE_ALL_WIDTH);
allFields[i].setCookie(i+"");
allFields[i].setFont(bodyFont);
allFields[i].setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField tempChoice = (ColorCheckBoxField)field;
int index =Integer.parseInt(tempChoice.getCookie().toString().trim());
//set the selection
if(tempChoice.getChecked()){
parent.selectInterest(index);
}
boolean flag = true;
int[] intrests = parent.getSelectedInterest();
for (int i = 0; i < intrests.length; i++) {
if(intrests[i]==0){
flag = false;
}
}
if(flag==true){
selectAll.setChecked(flag); // select all is Checkbox object
}else{
selectAll.setChecked(false);
}
}
});
vfm.add(allFields[i]);
}
我全选复选框逻辑是
selectAll = new ColorCheckBoxField("Select All", false, checkBoxStyle | USE_ALL_WIDTH);
selectAll.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField temp = (ColorCheckBoxField) field;
//if (context == FieldChangeListener.PROGRAMMATIC) {
checkAll(temp.getChecked()); // it loops through all checkbox and set them checked
//}
}
});
innerHfm.add(selectAll);
我明白的问题,它是由于无限循环。我已经使用了“FieldChangeListener.PROGRAMMATIC”,但这不会帮助,因为我希望字段监听器能够以实用和手动方式工作。我没有任何选择可以修复。任何黑客将帮助我?
@Eugen ..绝对是+1的答案。你救了我的一天。 –
@SmartTechie谢谢你提出这样的问题.. –
@Eugen谢谢你给出答案 –