0

I am trying to remove one object for an array stored in parse (e.g [user1],[user2],[user3], I was to change to [user1],[user3]). I have a module class which stores the different students on the module, when the user removes the module I want to remove their userID from the array stored in the students parse array.

If that is not possible, how can I retrieve the whole array into a local arraylist, edit it, then put it back?

I have attempted a few things but none has worked so far. this is my current code which deletes the whole row and not just the array:

moduleDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View temp = v;
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Module");
                query.whereEqualTo("moduleCode", getItem(position).getModuleCode());
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> module, ParseException e) {
                        if (e == null) {
                            for (ParseObject delete : module) {
                                delete.remove(currentUser.getObjectId());
                                delete.deleteInBackground();
                            }
                            Toast.makeText(temp.getContext(), "Deleted", Toast.LENGTH_LONG).show();
                        } else {
                            Log.e("Error", e.getMessage());
                            Toast.makeText(temp.getContext(), "Error deleting", Toast.LENGTH_LONG).show();
                        }
                    }
                });

            }
        });

Thanks for any help!

2 Answers 2

1

I found a way to do it which takes the array and stores it locally, changes it, then sends it back to parse. Not the best looking way but it worked!

moduleDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                students = new ArrayList<String>();
                final View temp = v;
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Module");
                query.whereEqualTo("moduleCode",getItem(position).getModuleCode());
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> list, ParseException e) {
                        if (e==null) {
                            if (list.size()>0) {
                                ParseObject s = list.get(0);
                                if (s.getList("students")!=null)
                                {
                                    students = s.getList("students");
                                }
                                else
                                {
                                    students = null;
                                }
                                if (students!=null) {
                                    if (students.contains(currentUser.getObjectId())) {
                                        students.remove(currentUser.getObjectId());
                                        s.put("students",students);
                                        s.saveInBackground(new SaveCallback() {
                                            @Override
                                            public void done(ParseException arg0) {
                                                Toast.makeText(temp.getContext(), "Deleted", Toast.LENGTH_LONG).show();
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }
                });
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

1

I've encountered the same issue, solved it a bit differently using removeAll(...). In my case I need to delete a ParseObject and then remove from an array inside another ParseObject - lets call it container.

    final ParseObject toBeDeleted = ParseObject.createWithoutData(OBJ_TYPE, ITEM_ID);

    final ArrayList<ParseObject> removeArr = new ArrayList<>();
    removeArr.add(toBeDeleted);

    ParseQuery<ParseObject> query = ParseQuery.getQuery(CONTAINER_OBJ);
    query.include(THE_ARRAY);
    query.getInBackground(protestID, new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                object.removeAll(THE_ARRAY, removeArr);
                object.saveInBackground();
            } else {
                // something went wrong
            }
        }
    });

    toBeDeleted.deleteInBackground(new DeleteCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.v(TAG, "deleted " + parseType + " successfully");

            }
        }
    });

I hope that will help anyone else who needs it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.