登録ページでユーザーがニュースカテゴリを選択できるシンプルなアプリを実装しています。要件は以下のとおりです
- すべてのカテゴリはCheckBoxFieldのものです。ユーザーは少なくとも1つのカテゴリを選択する必要があります。
- すべてのチェックボックスを選択すると、すべてのカテゴリのチェックボックスを選択/選択解除できます。
- ユーザーがすべてのチェックボックスフィールドを手動で選択する場合は、[すべて選択]チェックボックスを選択する必要があります。
アプローチ:ループ内にカテゴリチェックボックスを作成しました。
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チェックボックスロジックは
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」を使用しましたが、フィールドリスナーを実用的および手動の両方で機能させたいので、それは役に立ちません。修正するオプションが残っていません。どんなハックも私を助けますか?