我在 Parse.com 上有两个类(class):图像和用户。在用户类中,我保存了 3 个字段:姓名、手机号码和职业。在 Image 类中,我正在保存图像。
我必须在这两个类之间创建一对多关系,以便我可以使用相应的手机号码获取图像。
我在 parse.com 上创建了 User 类和 Image 类之间的关系,并在 User 类中创建了名为 Mobile 的列。无法找到如何获取具有一对多关系的特定手机号码的图像。请帮助,因为我已经阅读了文档。提前致谢。
最佳答案
这只是要处理的代码。您必须仔细考虑组织您的类(class)。我不确定您打算如何进行,但就涉及编码而言,以下内容应该有所帮助。
首先获取当前用户。
ParseUser currentUser = ParseUser.getCurrentUser();
由于“用户”表中的关系为“移动”列,因此首先获取该关系。 像这样。
ParseRelation<ParseObject> relation = currentUser
.getRelation("mobile");
一旦获得关系,就可以查询需要的对象。在你的例子中,它是图像对象。
ParseQuery<ParseObject> query = relation.getQuery("Image");
从这里,您可以自定义您的查询。例如:
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject, ParseException e) {
if(e == null)
//your code
//List<ParseFile> pFileList = (ArrayList<ParseFile>) object.get("images"); //something like this just to give an idea
}
});
或者如果您有多个对象,请使用
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
for (ParseObject object : list) {
ParseFile image = (ParseFile) object.get("images_column_name");
}
}
}
});
关于android - Parse.com 上的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21085873/