从Parse.com阵列中删除一个对象 [英] Remove one object from Parse.com Array column

查看:212
本文介绍了从Parse.com阵列中删除一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除一个对象存储在解析一个数组(如[USER1],[USER2],[用户3],我是更改为[USER1],[用户3])。我有一个存储模块上的不同的学生,当用户删除我想从存储在学生数组中删除自己的用户ID解析阵列模块,一个模块类

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.

如果这是不可能的,我怎么可以检索整个阵列到本地的ArrayList,编辑它,然后把它放回去?

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

我已经尝试了一些东西,但至今没有工作过。这是我目前的code这将删除整行,而不仅仅是数组:

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

            }
        });

感谢您的帮助!

推荐答案

我找到了一种方法做这需要数组并将其存储在本地,更改它,然后将它发送回来解析。没有最好看的方式,但它的工作!

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

这篇关于从Parse.com阵列中删除一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆