Android 使用指针获取父对象的子对象
当我有一个孩子的父母时,我正试图得到一个孩子。 我创建了一个子对象(注释)、一个父对象(post)和一个从子对象指向父对象的指针-Android 使用指针获取父对象的子对象,android,parse-platform,Android,Parse Platform,当我有一个孩子的父母时,我正试图得到一个孩子。 我创建了一个子对象(注释)、一个父对象(post)和一个从子对象指向父对象的指针- // Create the post ParseObject myPost = new ParseObject("Post"); myPost.put("title", "I'm Hungry"); myPost.put("content", "Where should we go for lunch?"); // Cre
// 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());
}
}
});
String t=”“;
解析对象c;
解析查询=
getQuery(“注释”);
查询:whereEqualTo(“母公司”,myPost);
findInBackground(新的FindCallback(){
@凌驾
公共作废完成(列表,com.parse.ParseException e){
如果(e==null){
c=list.get(0);
t=c.getString(“内容”);
Toast.makeText(getApplicationContext(),t,Toast.LENGTH_LONG.show();
}否则{
Log.d(“NY”,“Model.getStudentById错误:”+e.getMessage());
}
}
});
但我没有得到评论。
我也试着提出疑问;但它没有起作用
我该怎么办??
谢谢我能弄明白的唯一一件事是,您可能正在尝试从另一个方法检索
myPost
对象,然后才将其实际保存以进行解析
当我将您的代码复制到测试项目中时,我无法让它工作。这里有一个更新版本,确实有效
注意:我正在链接这些操作,以便在执行下一个操作之前确定(好吧,尽可能确定……您仍然需要良好的错误/异常处理)
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
初始化(这个,“,”);
createPost();
}
私有void createPost(){
ParseObject myPost=新的ParseObject(“Post”);
myPost.put(“标题”,“我饿了”);
放置(“内容”,“我们应该去哪里吃午饭?”);
mPost=myPost;
ParseObject myComment=新的ParseObject(“注释”);
myComment.put(“内容”,“让我们做寿司吧”);
myComment.put(“父”,myPost);
myComment.saveInBackground(新的SaveCallback(){
@凌驾
公共作废完成(Parsee异常){
ParseQuery=ParseQuery.getQuery(“注释”);
查询:whereEqualTo(“母公司”,mPost);
findInBackground(新的FindCallback(){
@凌驾
公共作废完成(列表,com.parse.ParseException e){
如果(e==null){
ParseObject c=list.get(0);
字符串t=c.getString(“内容”);
Toast.makeText(getApplicationContext(),t,Toast.LENGTH_LONG.show();
}否则{
Log.d(“NY”,“Model.getStudentById错误:”+e.getMessage());
}
}
});
}
});
}
上面的代码显示了我的“Hello World”活动,然后发布了一个祝酒词,上面写着“让我们做寿司吧。”在您的代码中,您永远不会保存
myPost
。您能否确认(通过Parse Dashboard)您的“Post”对象实际上正在创建?ParseObject myPost=新的ParseObject(“Post”);myPost.put(“标题”,“我饿了”);myPost.put(“内容”,“我们应该去哪里吃午饭?”);-这段代码保存对象并在解析中创建表。我在解析中检查了它,并看到了它。
@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());
}
}
});
}
});
}