解析android一对多的关系

时间:2015-02-01 18:56:17

标签: java android parse-platform

我正在尝试使用parse为特定帖子设置评论,我已经在parse.com数据中有一个Post类和一个Comment类,无论如何我试着在类的列中设置注释然后获取它,但问题是,我每篇文章只能得到1条评论,怎么做,是我的问题,我尝试了一对多的关系,但它没有用,我试了一个指针在Meal类中指向Comment类的行,但我不知道该怎么做,这里有一些示例代码:

 public void addTheComment() {



    // Create the Post object
    ParseObject post = new ParseObject("Post");
    post.put("textContent", txtComment.getText().toString());

    // Create an author relationship with the current user
    post.put("comment", getCurrentMeal());

    // Save the post and return
    post.saveInBackground(new SaveCallback () {

        @Override
        public void done(ParseException e) {
            if (e == null) {
                setResult(RESULT_OK);
                finish();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Error saving: " + e.getMessage(),
                        Toast.LENGTH_SHORT)
                        .show();
            }
        }

    });

这会在按下时通过按钮添加编辑文本中的注释

以下是显示评论的列表:

 private void updateComments() {
    ParseQueryAdapter<ParseObject> adapter =
            new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
                public ParseQuery<ParseObject> create() {
                    // Here we can configure a ParseQuery to our heart's desire.
                    ParseQuery query = new ParseQuery("Post");
                    query.whereEqualTo("comment", getCurrentMeal());
                    return query;
                }
            });


    adapter.setTextKey("comment");
    adapter.setImageKey("photo");
    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);
}

和:

public Meal getCurrentMeal() {
        return meal;
    }

1 个答案:

答案 0 :(得分:0)

 private void updateComments() {
    ParseQueryAdapter<ParseObject> adapter =
            new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
                public ParseQuery<ParseObject> create() {
                    // Here we can configure a ParseQuery to our heart's desire.
                    ParseQuery query = new ParseQuery("Comment");
                    query.whereContainedIn("parent", Arrays.asList(mealId));
                    return query;
                }
            });


    adapter.setTextKey("content");
    //  adapter.setImageKey("photo");
    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);
}

当按下按钮(激活方法addTheComment)时,它会添加编辑文本(txtComment)中的文本,将其插入到与Post类建立关系的Comment部分。

public void addTheComment() {


// Create the comment
        ParseObject myComment = new ParseObject("Comment");
        myComment.put("content", txtComment.getText().toString());

// Add a relation between the Post with objectId "1zEcyElZ80" and the comment
        myComment.put("parent", ParseObject.createWithoutData("Meal", mealId));

// This will save both myPost and myComment
        myComment.saveInBackground();

    }