使用指针获取 parent 的 child

Get a child of a parent using pointer

当我有他的 parent 时,我正试图得到一个 child。 我创建了一个 child (comment) ,一个 parent (post) 和一个从 child 到 parent-

的指针
    // Create the post
    ParseObject myPost = new ParseObject("Post");
    myPost.put("title", "I'm Hungry");
    myPost.put("content", "Where should we go for lunch?");


    // Create the comment
    ParseObject myComment = new ParseObject("Comment");
    myComment.put("content", "Let's do Sushirrito.");

    // Add a relation between the Post and Comment
    myComment.put("parent", myPost);

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

我的查询:

String t="";
ParseObject c;

 ParseQuery<ParseObject> query =
      ParseQuery.getQuery("Comment");
       query.whereEqualTo("parent",myPost );   
       query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list,com.parse.ParseException e) {
              if (e == null) {
                c= list.get(0);
                t= c.getString("content");
                Toast.makeText(getApplicationContext(),  t , Toast.LENGTH_LONG).show();                                                 
                } else {
                    Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
                    }

                }
      });

但我没有收到评论。 我也试着把 query.include("parent");但是没有用。

我该怎么办?? 谢谢

我唯一能弄清楚的是,您可能试图在实际保存到 Parse 之前从另一个方法检索 myPost 对象。

当我将您的代码复制到测试项目中时,我无法让它工作。这是 有效的更新版本。

注意: 我正在链接操作,以便我确定(好吧,就像我可以...你仍然需要良好的 error/exception 处理)上一个操作在执行下一个操作之前完成。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Parse.initialize(this, "<value>", "<value>");
    createPost();
}

private void createPost() {
    ParseObject myPost = new ParseObject("Post");
    myPost.put("title", "I'm Hungry");
    myPost.put("content", "Where should we go for lunch?");

    mPost = myPost;

    ParseObject myComment = new ParseObject("Comment");
    myComment.put("content", "Let's do Sushirrito.");

    myComment.put("parent", myPost);

    myComment.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Comment");
            query.whereEqualTo("parent", mPost);
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> list, com.parse.ParseException e) {
                    if (e == null) {
                        ParseObject c = list.get(0);
                        String t = c.getString("content");
                        Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
                    } else {
                        Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
                    }
                }
            });
        }
    });
}

上面的代码显示了我的 "Hello World" Activity 然后发布了一个 Toast 说 "Let's do Sushirrito."