在我的应用程序中,我目前有一个ListView适配器连接到我制作的Records类,我想要实现的是从我的JSONObject中获取布尔值并将它们与我的Checkbox同步。
ChecklistActivity.java
public class ChecklistActivity extends AppCompatActivity {
Toolbar toolbar;
private ChecklistAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAdapter = new ChecklistAdapter(this);
ListView listView = (ListView) findViewById(R.id.list1);
listView.setAdapter(mAdapter);
fetch();
}
private void fetch() {
Intent intent = getIntent();
String passedURL = intent.getStringExtra("jsonData");
JsonObjectRequest request = new JsonObjectRequest(
passedURL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
List<Record> records = parse(jsonObject);
mAdapter.swapImageRecords(records);
}
catch(JSONException e) {
Toast.makeText(ChecklistActivity.this, "Unable to obtain data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(ChecklistActivity.this, "Unable to fetch data: " + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
ParseApplication.getInstance().getRequestQueue().add(request);
}
private List<Record> parse(JSONObject json) throws JSONException {
ArrayList<Record> records = new ArrayList<Record>();
Intent iin= getIntent();
Bundle b = iin.getExtras();
String arrayName =(String) b.get("jsonArray");
JSONArray jsonArray = json.getJSONArray(arrayName);
for(int i =0; i < jsonArray.length(); i++) {
JSONObject jsonImage = jsonArray.getJSONObject(i);
String id = jsonImage.getString("id");
String value = jsonImage.getString("value");
// Checkbox
JSONObject jsonObject = ParseUser.getCurrentUser().getJSONObject("checklistData");
Boolean checkBoxValue = jsonObject.getBoolean(id);
Record record = new Record(id, value, checkBoxValue);
records.add(record);
}
return records;
}
}
Record.java
public class Record {
private String id;
private String value;
private boolean checkBoxValue;
public Record(String id, String value, boolean checkBoxValue) {
this.id = id;
this.value = value;
this.checkBoxValue = checkBoxValue;
}
public String getID() {
return id;
}
public String getValue() {
return value;
}
public boolean getCheckBoxValue() {
return checkBoxValue;
}
}
ChecklistAdapter.java
public class ChecklistAdapter extends ArrayAdapter<Record> {
CheckBox checkBox;
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) convertView.findViewById(R.id.checkBox);
final Record dataRecord = getItem(position);
desc.setText(dataRecord.getValue());
checkBox.setChecked(dataRecord.getCheckBoxValue());
final JSONObject myObject = new JSONObject();
try {
myObject.put(dataRecord.getID(), true);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonObject = ParseUser.getCurrentUser().getJSONObject("checklistData");
checkBox.setOnClickListener(new View.OnClickListener() {
String idSelected = dataRecord.getID();
public void onClick(View v) {
boolean checkBoxValue = true;
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();
}
}
从我的JSONObject分配布尔值时,问题似乎出现在ChecklistActivity.java
文件中。
如ChecklistActivity.java
中所见,我声明了一个看起来像这样的JSONObject
{ "1ed1": true, "1ed2": false, "1ed3": true, "1ed4": false, "1ep1": true, "1ed2": false, "1ep3": true, "1c1": false, "1c2": true, "1c3": false, "1c4": true, "1c5": false, "1c6": true, "2ed1": false, "2ed2": true, "2ed3": false, "2ep1": true, "2ep2": false, "2ep3": true, "2c1": false, "2c2": true, "2c3": false, "2c4": true, "2c5": false, "2c6": true, "3ed1": false, "3ed2": true, "3ed3": false, "3ep1": true, "3ep2": false, "3c1": true, "3c2": false, "3c3": true, "3c4": false, "3c5": true, "3c6": false, "4ed1": true, "4ed2": false, "4ed3": true, "4ep1": false, "4ep2": true, "4c1": false, "4c2": true, "4c3": false, "4c4": true, "4c5": false }
在该文件中有两个String
id等于JSONObject中的键 value等于JSONObject中的值
这两个都工作正常
我试图为每个checkBox分配一个布尔值,所以为了让我这样做,我必须声明一个正确的布尔值来检索一个JSON键及它的布尔值。
答案 0 :(得分:0)
尝试将获取值更改为字符串:
Boolean checkBoxValue = jsonObject.optString(id).equals("true");
答案 1 :(得分:0)
我可以通过在ChecklistAdapter.java
类
JSONObject jsonObject = ParseUser.getCurrentUser().getJSONObject("checklistData");
String id = dataRecord.getID();
Boolean checkIDValue = jsonObject.optString(id).equals("true");
checkBox.setTag(getItem(position));
if (checkIDValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}