android Parse.com 中的关系 [英] Relationship in android Parse.com

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

问题描述

拜托,我真的需要你的帮助,我正在实现一个带有标签功能的应用,就像 twitter 一样.我正在使用 parse.com 并且我有一个 Photo 类和 Tag 类.所以用户在文本字段中写入标签名称,当用户点击上传照片按钮时,两个类之间创建了一个关系,所以当我点击一个标签时,所有用该标签标记的照片都会显示出来.

Please I really need your help, I'm implementing an app with tagging feature just like twitter. I'm using parse.com and I have a class Photo and class Tag. So the user write the tag name in the text field and when the user click the upload photo button a relation created between the 2 classes, so when I click a tag, all the photo tagged with that tag will be displayed.

   upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final ParseQuery<ParseObject> query = ParseQuery.getQuery("Photo");
            query.getInBackground(objectId, new GetCallback<ParseObject>() {
                public void done(final ParseObject object, ParseException e) {
                    if (e == null) {
                        final ParseObject tag= new ParseObject("Tags");
                        tag.put("tag_name", addName.getText().toString());

                        final ParseObject photo = new ParseObject("Photo");

                        photo.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e == null) {
                                    ParseRelation relation = tag.getRelation("photo");
                                    relation.add(photo);
                                    tag.saveInBackground();
                                }
                            }
                        });
        }
    });

任何建议将不胜感激<3

any suggestions will be appreciated <3

推荐答案

您每次都明确地创建一个新标签.而不是这样做,您应该查询具有相同名称的标签并连接它们或在找不到任何匹配项时创建新标签.

You're explicitly creating a new tag each time. Instead of doing that you should be querying for the tags with the same name and connecting them or creating new ones if you don't find any matches.

这篇关于android Parse.com 中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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