Parse.com Android中的一对多关系

时间:2015-07-09 05:35:21

标签: java android database parse-platform relational-database

我在Parse.com中遇到了android实现一对多关系的问题。我正在尝试使用指针保存 Category Questions 之间的关系。我可以得到指针( ID 的问题),但我不知道如何获取他们指向的数据。是否有任何方法可以获取它们或者我必须在问题类上调用查询( ID 问题我来自 Cathegory 类)?

我保存关系的代码:

            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中获得了这个模型:

类别

问题类

我检索到目前为止所写数据的代码:

   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");
                }
            });

0 个答案:

没有答案