I have an adapter class with a checkbox and when the checkbox is selected it pushes a JSONObject key and value to Parse. Inside my app it only saves one key and value to Parse as a JSONObject and I want my app to save several key and values into Parse when selecting other Checkboxes.
When I select a different Checkbox it changes the single key and value rather then adding another set to the JSONObject
Photo of the row inside Parse including the JSONObject
Instead I'd like to have the JSONObject inside Parse to save data like this when other checkboxes with different keys and values are selected.
{"2c1":true, "2c2":true, "2c3":true, "2c4":true, "2c5":true, "2c6":true}
instead of just having this alone
{"2c1":true}
Here is the code inside my Adapter class
ChecklistAdapter.java
final JSONObject myObject = new JSONObject();
try {
myObject.put(dataRecord.getID(), true);
} catch (JSONException e) {
e.printStackTrace();
}
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();
}
}
});
The functional code provided just saves on key and value pair.
Edit: If you're curious about what the Toast does when you click the Checkbox here.
final JSONObject myObject = new JSONObject();
checkBox.setOnClickListener(new View.OnClickListener() {
String idSelected = dataRecord.getID();
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
try {
myObject.put(dataRecord.getID(), true);
} catch (JSONException e) {
e.printStackTrace();
}
ParseUser parseUser= ParseUser.getCurrentUser();
parseUser.put("checklistData", myObject);
parseUser.saveInBackground();
Toast.makeText(getContext(), idSelected,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "CheckBox is unchecked",
Toast.LENGTH_SHORT).show();
}
}
});
In your OnClickListener, you are not updating the object you are sending to the parse database. So the object you are sending to your parse database is not getting updated. The data is set for your JSON Object only once outside your OnClickListener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With