Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several JSONObject keys and values won't save to Parse

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.

enter image description here

like image 856
pxtvr Avatar asked Dec 14 '15 02:12

pxtvr


2 Answers

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();

        }
    }
});
like image 91
Kishore Jethava Avatar answered Sep 21 '22 15:09

Kishore Jethava


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.

like image 29
Horatio Avatar answered Sep 23 '22 15:09

Horatio