在嵌套在for循环中的第一个查询完成后执行parse.com查询

时间:2015-11-08 21:09:46

标签: java android parse-platform

我正在使用parse.com作为我的后端数据库在Java中创建一个Android应用程序。 我必须解析查询,一个嵌套在for循环中,另一个在for循环之外

for (int x = 0; x <= Classes.size() - 1; x++) {
                    final ParseQuery<ParseObject> classesQuery = ParseQuery.getQuery("Classes")
                            .whereEqualTo("classcode", Classes.get(x).classcode)
                            .whereEqualTo("schoolcode", sStart.schoolcode);
                    classesQuery.orderByAscending("classcode");

                    final int finalX = x;

                    classesQuery.findInBackground(new FindCallback<ParseObject>() {
                        @Override
                        public void done(List<ParseObject> list, ParseException e) {
                            if (e == null) {

                                UserClasses test = new UserClasses();
                                test.classcode = Classes.get(finalX).classcode;
                                test.subject = list.get(0).getString("subject");
                                test.teacherUsername = list.get(0).getString("teacher");
                                test.Curriculum = list.get(0).getString("curriculum");
                                Classes.set(finalX, test);


                            } else {
                                System.out.println("There was a problem with the query to get user's classes subject name, teacher username and curriculum code from the database");
                            }
                        }
   final ParseQuery<ParseObject> lessonsQuery = ParseQuery.getQuery("Lessons")
                                        .whereEqualTo("classcode", item.classcode)
                                        .whereEqualTo("schoolcode", sStart.schoolcode)
                                        .whereGreaterThanOrEqualTo("endtime", date);
                                lessonsQuery.countInBackground(new CountCallback() {
                                    @Override
                                    public void done(int i, ParseException e) {
                                        if (e == null) {
                                            if (i > 1) {
                                                lessonsQuery.findInBackground(new FindCallback<ParseObject>() {
                                                    @Override
                                                    public void done(List<ParseObject> list, ParseException e) {
                                                        if (e == null) {
                                                            UserLessons test = new UserLessons();
                                                            test.subject = item.subject;
                                                            test.classcode = item.classcode;
                                                            test.teacherUsername = item.teacherUsername;
                                                            test.Curriculum = item.Curriculum;
                                                            for (int x = 0; x <= list.size(); x++) {
                                                                test.lessoncode = list.get(x).getString("lessoncode");
                                                                test.venuecode = list.get(x).getString("venuecode");
                                                                test.startTime = list.get(x).getDate("starttime");
                                                                test.endTime = list.get(x).getDate("endtime");
                                                                Calendar newStartTime = Calendar.getInstance();
                                                                newStartTime.setTime(test.startTime);
                                                                Calendar newEndTime = Calendar.getInstance();
                                                                newEndTime.setTime(test.endTime);
                                                                if (newStartTime.get(Calendar.DATE) == today.get(Calendar.DATE)) {
                                                                    Lessons.add(test);

                                                                }
                                                            }

                                                        } else {
                                                            System.out.println("There was an error with the query to get user's classes lessoncode, venuecode, startime and endtime");
                                                        }
                                                    }
                                                });

我不想将其他parse.com查询放在for循环中,因为它会运行几次。有没有办法,我可以调用第二个查询,以便在第一个查询完成时运行。我试着使用计时器,但这让我陷入困境,有办法吗?

0 个答案:

没有答案