我正在使用Android Studio中的某个程序。我试图从多个复选框中获取值(35)并将它们放入JSON中,以便将其发送到服务器。
以下是获取所选复选框值的SetMethod
:
private void setExtensionForwardNumber(){
priorityResponse.priorityOne.getTimeTable().mon = verifyCheckbox(priorityOneMon);
priorityResponse.priorityOne.getTimeTable().tue = verifyCheckbox(priorityOneTue);
priorityResponse.priorityOne.getTimeTable().wed = verifyCheckbox(priorityOneWed);
priorityResponse.priorityOne.getTimeTable().thu = verifyCheckbox(priorityOneThur);
priorityResponse.priorityOne.getTimeTable().fri = verifyCheckbox(priorityOneFri);
priorityResponse.priorityOne.getTimeTable().sat = verifyCheckbox(priorityOneFri);
priorityResponse.priorityOne.getTimeTable().sun = verifyCheckbox(priorityOneSun);
String s = priorityResponse.toJsonObject();
setForwardExternalFragment(s);
}
以下是验证复选框是否已选中的函数:
private boolean verifyCheckbox(CheckBox checkbox) {
boolean selected = checkbox.isChecked();
if(selected)
checkbox.setChecked(true);
else
checkbox.setChecked(false);
return selected;
}
我尝试不使用函数verifyCheckbox
,我使用了checkbox.isChecked()
,但我得到了相同的结果。
priorityResponse.priorityOne.getTimeTable().tue = priorityOneTue.isChecked();
问题是当我在服务器上收到值时,我得到混合值。示例:如果我在服务器上检查了mon,wed,fri,sun,则所选值为:mon,tue,wed,thu,sun。我哪里出错了?
答案 0 :(得分:0)
做一件事。在每个复选框上附加一个检查侦听器,并将复选框值附加到全局变量。并将附加值发送到服务器。
String str; //global variable.
priorityOneTue = (CheckBox)findViewById(R.id.priorityOneTue);
priorityOneTue.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
str += "tue";
}
});
然后将str发送到服务器
setForwardExternalFragment(str);