【发布时间】:2015-12-05 16:15:00
【问题描述】:
我有一个带有布尔值和不同字符串的 JSONObject,它看起来很像这样
{"1c1":false,"1c2":false,"1c3":false,"1c4":false,"1ed1":false,"1ed2":false,"1ed3":false,"1ep1":true,"2c1":true,"2c2":true,"2c3":false,"2ed1":false,"2ed2":false,"2ed3":true}
每个 ListView 项的值至少等于 JSONObject 中的一个字符串
ChecklistAdapter.java
public class ChecklistAdapter extends ArrayAdapter<Record> {
public ChecklistAdapter(Context context) {
super(context, R.layout.checklist_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.checklist_item, parent, false);
}
TextView desc = (TextView) convertView.findViewById(R.id.textView9);
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
final JSONObject fetchedJSON = ParseUser.getCurrentUser().getJSONObject("checklistData");
final Record dataRecord = getItem(position);
desc.setText(dataRecord.getValue());
final JSONObject myObject = new JSONObject();
// if item id in list view is equal to one of the strings in the json object make sure the checkbox is selected as true
checkBox.setOnClickListener(new View.OnClickListener() {
String idSelected = dataRecord.getID();
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
ParseUser.getCurrentUser().put("checklistData", myObject);
ParseUser.getCurrentUser().saveInBackground();
Toast.makeText(getContext(), idSelected,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "CheckBox is unchecked",
Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
public void swapImageRecords(List<Record> objects) {
clear();
for (Record object : objects) {
add(object);
}
notifyDataSetChanged();
}
}
正如在代码中看到的,当单击 ListView 项时,它会返回给定的选定 ID,该 ID 等于 JSONObject 中的字符串之一
String idSelected = dataRecord.getID();
我的计划是如果“idSelected”等于 JSONObject 中的字符串之一,请检查它是真还是假布尔值,并将复选框选择为真值。
【问题讨论】:
标签: java android json android-studio