Parse.com one-to-many 关系 Android
Parse.com one-to-many relationship in Android
我在 Parse.com 中 android 实现 one-to-many 关系时遇到问题。我正在尝试使用指针保存 Category 和 Questions 之间的关系。我可以获得指针(IDs of Question)但我不知道如何获取它们指向的数据。有没有什么方法可以获取它们,或者我是否必须在 Questions class 上调用查询(ID of 问题 我从 Cathegory class) 得到 ?
我保存关系的代码:
Question q1 = new Question("How much money cost new Skoda Fabia?", "300 " +
"000kc", "200 000kc", "100 000kc", "50 000kc");
q1.saveInBackground();
Question q2 = new Question("How much money cost new Skoda Felicia?", "300 " +
"000kc", "200 000kc", "100 000kc", "150 000kc");
q2.saveInBackground();
Question q3 = new Question("How much money cost new Skoda Octavia?", "500 " +
"000kc", "150 000kc", "100 000kc", "50 000kc");
q3.saveInBackground();
ArrayList<Question> questionArrayList = new ArrayList<>();
questionArrayList.add(q1);
questionArrayList.add(q2);
questionArrayList.add(q3);
ParseObject parseObject = new ParseObject("Cathegory");
parseObject.put("Auto", questionArrayList);
parseObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.d(TAG, "Save was sucesfully");
Log.d(TAG, "error was ? : " + (e == null ? "none" : e.getMessage()));
}
});
调用后我在 Parse.com 中得到了这个模型:
类别class

问题class

我目前写的检索数据的代码:
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Cathegory");
parseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
List<Question> questionList = parseObject.getList("Auto");
//I have IDs of Question in questionList
for(Question q : questionList){
q.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
//Still not have all information about Question (just ID)
Log.d(TAG,"Data parseObject retrieved");
}
});
}
Log.d(TAG,"Data retrieved");
}
});
我通过不使用 List 而只使用从 Question 指向 Category 的简单指针来解决这个问题(上面是相反的)
然后我简单地创建类别
Category sports = new Category("Sports");
...
sports.save();
然后我使用这个 Category 作为 Question 函数
中的参数
setCategory(Category cat){
put("IdCategory",cat);
}
我不知道如何在 Android 中使用它,但我正在使用云代码 (JavaScript),它看起来像这样 :
queryOnQuestion.equalTo("IdCategory", {
__type: "Pointer",
className: "Category",
objectId: allowedCat.get("IdCategory").id
});
我希望 Android 看起来相似。在最坏的情况下,您可以使用这部分代码定义云函数。
希望对某人有所帮助。
我在 Parse.com 中 android 实现 one-to-many 关系时遇到问题。我正在尝试使用指针保存 Category 和 Questions 之间的关系。我可以获得指针(IDs of Question)但我不知道如何获取它们指向的数据。有没有什么方法可以获取它们,或者我是否必须在 Questions class 上调用查询(ID of 问题 我从 Cathegory class) 得到 ?
我保存关系的代码:
Question q1 = new Question("How much money cost new Skoda Fabia?", "300 " +
"000kc", "200 000kc", "100 000kc", "50 000kc");
q1.saveInBackground();
Question q2 = new Question("How much money cost new Skoda Felicia?", "300 " +
"000kc", "200 000kc", "100 000kc", "150 000kc");
q2.saveInBackground();
Question q3 = new Question("How much money cost new Skoda Octavia?", "500 " +
"000kc", "150 000kc", "100 000kc", "50 000kc");
q3.saveInBackground();
ArrayList<Question> questionArrayList = new ArrayList<>();
questionArrayList.add(q1);
questionArrayList.add(q2);
questionArrayList.add(q3);
ParseObject parseObject = new ParseObject("Cathegory");
parseObject.put("Auto", questionArrayList);
parseObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.d(TAG, "Save was sucesfully");
Log.d(TAG, "error was ? : " + (e == null ? "none" : e.getMessage()));
}
});
调用后我在 Parse.com 中得到了这个模型:
类别class
问题class
我目前写的检索数据的代码:
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Cathegory");
parseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
List<Question> questionList = parseObject.getList("Auto");
//I have IDs of Question in questionList
for(Question q : questionList){
q.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
//Still not have all information about Question (just ID)
Log.d(TAG,"Data parseObject retrieved");
}
});
}
Log.d(TAG,"Data retrieved");
}
});
我通过不使用 List 而只使用从 Question 指向 Category 的简单指针来解决这个问题(上面是相反的)
然后我简单地创建类别
Category sports = new Category("Sports");
...
sports.save();
然后我使用这个 Category 作为 Question 函数
中的参数 setCategory(Category cat){
put("IdCategory",cat);
}
我不知道如何在 Android 中使用它,但我正在使用云代码 (JavaScript),它看起来像这样 :
queryOnQuestion.equalTo("IdCategory", {
__type: "Pointer",
className: "Category",
objectId: allowedCat.get("IdCategory").id
});
我希望 Android 看起来相似。在最坏的情况下,您可以使用这部分代码定义云函数。
希望对某人有所帮助。