用户属性,无协调的随机对象..为什么不起作用?为什么没有21件物品?

时间:2017-05-01 22:22:09

标签: java android arraylist request backendless

我有一个复杂的问题,我不明白。在这堂课我想要 从一个arraylist listChallenges添加21个随机对象到arraylist finalChallenges。然而它并不起作用,有时finalChallanges包含21个对象,但大多数时候它包含的对象较少,但我不知道问题出在哪里。实际上,我试图评论每一步,如果做错了,请告诉我。 请帮帮我,我不知道我该怎么做..

 ArrayList<Challenges>          listChallenges  = new ArrayList<Challenges>();
 ArrayList<Challenges>          finalChallenges = new ArrayList<Challenges>(20);

//Check where the same userId and subscribers.objectId,
    //Request these categories object and save to the  ArrayList<Category> totalCategories
    //Save these categories objectId to the selectedCategoriesId List<String>
    BackendlessDataQuery query = new BackendlessDataQuery();
    query.setWhereClause( "subscribers.objectId = '"+backendlessUser.getObjectId()+"'");

    Backendless.Data.of(Category.class).find(query, new AsyncCallback<BackendlessCollection<Category>>() {
        @Override
        public void handleResponse(BackendlessCollection<Category> categoriesBackendlessCollection) {
            //add selected categories to totalActivities Category ArrayList
            for( Category categories : categoriesBackendlessCollection.getData()) {
                totalCategories.add(categories);
                selectedCategoriesId.add(categories.getObjectId());
               //
            }
            System.out.println(selectedCategoriesId);

            //For cycle is going to selectedCategoriesId.size
            //Check where the same category-objectId and actual selectedCategoriesId
            //Request these challenges object, which are in the actual category and save to the  ArrayList<Challenges> listChallenges
            //Save these categories objectId to the selectedCategoriesId List<String>
            for(int k=0;k<selectedCategoriesId.size();k++) {

                BackendlessDataQuery query = new BackendlessDataQuery();
                query.setPageSize(pageSize);
                query.setWhereClause("category.objectId = '" + selectedCategoriesId.get(k) + "'");

                Backendless.Data.of(Challenges.class).find(query, new AsyncCallback<BackendlessCollection<Challenges>>() {
                    @Override
                    public void handleResponse(BackendlessCollection<Challenges> cha) {
                        for (Challenges challenges : cha.getData()) {
                            listChallenges.add(challenges);

                            challengeTitle.add(challenges.getChallengeTitle());
                            challengeContent.add(challenges.getChallengeContent());
                            challangeId.add(challenges.getObjectId());
                        }
                        System.out.println("osszes elem:"+listChallenges);
                        //ArrayList<Challenges> finalChallenges  size is 21 with 0
                        //  get from listChallenges random 21 object without concord and add to the finalChallenges
                        Random random = new Random();
                        List<Challenges> temp = new ArrayList<>(listChallenges);
                        ArrayList<Challenges> tempNewList = new ArrayList<Challenges>();
                        //ArrayList<Challenges> temp = new ArrayList<Challenges>(listChallenges.size());
                        for (Challenges item : listChallenges) temp.add(item);
                        while (finalChallenges.size()<21 && temp.size()>0) {
                               int index = random.nextInt(temp.size());
                               tempNewList.add(temp.get(index));
                               temp.remove(index);
                            finalChallenges= tempNewList;
                        }

                       // System.out.println("kihívások");
                        System.out.println(finalChallenges);
                        System.out.println(finalChallenges.size());

                       // title.setText(challengeTitle.get(0));
                       // content.setText(challengeContent.get(0));
                       // objectId = challangeId.get(0);

                    }

                    @Override
                    public void handleFault(BackendlessFault fault) {

                    }
                });

                //save finalChallenges array objects to the current user "userChallenges" relationship
                Backendless.UserService.login( email, password, new AsyncCallback<BackendlessUser>() {
                    @Override
                    public void handleResponse(BackendlessUser backendlessUser) {

                        backendlessUser.setProperty("userChallenges",new ArrayList<>(finalChallenges));


                            }
                        });

                        Backendless.UserService.update(backendlessUser, new BackendlessCallback<BackendlessUser>() {
                            @Override
                            public void handleResponse(BackendlessUser response) {
                                System.out.println( "User has been updated" );
                            }

                            @Override
                            public void handleFault(BackendlessFault fault) {
                                System.out.println( "User has not been updated");
                            }
                        });

                    }

                    @Override
                    public void handleFault(BackendlessFault backendlessFault) {
                        System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
                    }
                },true);
            }


        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

1 个答案:

答案 0 :(得分:0)

您不会显示初始化finalChallenges的位置,我们可以看到它在响应处理程序中异步覆盖:很可能是并发访问的问题。

如果finalChallenges的实例不是&#34;共享&#34;那么从另一个列表的随机元素填充列表的逻辑本身是正确的。跨处理程序的不同/并发执行。

另外,一个小提示:要在没有循环的情况下一次性创建临时列表,您可以执行此操作:List<Challenge> temp = new ArrayList<>(listChallenges);

编辑:2条建议。

  1. 填写时在循环中使用临时列表,然后交换列表原子listChallenges = tempNewList
  2. 当您将列表传递给用户属性时,传递副本backendlessUser.setProperty("userChallenges", new ArrayList<>(finalChallenges));