Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用户属性,无协和的随机对象。。为什么';不管用?为什么';你拿不到21件东西?_Java_Android_Arraylist_Request_Backendless - Fatal编程技术网

Java 用户属性,无协和的随机对象。。为什么';不管用?为什么';你拿不到21件东西?

Java 用户属性,无协和的随机对象。。为什么';不管用?为什么';你拿不到21件东西?,java,android,arraylist,request,backendless,Java,Android,Arraylist,Request,Backendless,我有一个复杂的问题,我不明白。在这门课上,我想 将一个arraylist挑战中的21个随机对象添加到arraylist最终挑战。但是它不起作用,有时FinalChallenges包含21个对象,但大多数情况下它包含的对象较少,但我不知道问题出在哪里。事实上,我试着评论每一步,如果你做错了什么,请告诉我。 请帮帮我,我不知道该怎么办 ArrayList<Challenges> listChallenges = new ArrayList<Challenges

我有一个复杂的问题,我不明白。在这门课上,我想 将一个arraylist挑战中的21个随机对象添加到arraylist最终挑战。但是它不起作用,有时FinalChallenges包含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) {

        }
    });
ArrayList listChallenges=new ArrayList();
ArrayList finalChallenges=新ArrayList(20);
//检查相同的userId和subscribers.objectId,
//请求这些类别对象并保存到ArrayList totalCategories
//将这些类别objectId保存到selectedCategoriesId列表中
BackendlessDataQuery=新的BackendlessDataQuery();
query.setwhere子句(“subscribers.objectId=”+backendlessUser.getObjectId()+”);
backhandle.Data.of(Category.class).find(查询,新建AsyncCallback()){
@凌驾
公共无效句柄响应(BackendlessCollection类别BackendlessCollection){
//将所选类别添加到totalActivities类别ArrayList
对于(类别类别:Category BackEndlessCollection.getData()){
totalCategories.add(类别);
selectedCategoriesId.add(categories.getObjectId());
//
}
System.out.println(selectedCategoriesId);
//For cycle将转到selectedCategoriesId.size
//检查同一类别objectId和实际selectedCategoriesId的位置
//请求这些挑战对象,它们位于实际类别中,并保存到ArrayList listChallenges
//将这些类别objectId保存到selectedCategoriesId列表中

对于(int k=0;k您没有显示
finalChallenges
的初始化位置,我们可以看到它在响应处理程序中被异步覆盖:这很可能是并发访问的问题

如果
finalChallenges
的实例未在该处理程序的不同/并发执行之间“共享”,则从另一个列表的随机元素填充列表的逻辑本身是正确的

还有一个小提示:要一次性创建临时列表,您可以这样做:
list temp=new ArrayList(listChallenges);

编辑:2条建议

  • 填充循环时在循环中使用临时列表,然后以原子方式交换列表(
    listChallenges=tempNewList
  • 将列表传递给用户属性时,传递一份副本(
    backendlessUser.setProperty(“userChallenges”,new ArrayList(finalChallenges));

  • 谢谢你的回答。我编辑过。事实上效果很好,我会接受你的回答,但是你能帮我为什么要将用户属性设置为较少的对象吗?例如finalCHallenges conatins 21,而属性仅添加了12,如果你能帮上忙,我将非常抱歉我不理解你的问题。现在已经很晚了,我现在必须离开,但明天如果需要,我可以提供更详细的解释。非常感谢,我编辑了问题中的代码,现在看起来如何。编辑是为了第二个建议。我没有看到最初你通过列表来更新服务器端。我想这也是异步处理的。谢谢你的帮助,你非常有帮助…更新我thod从服务器端更新。事实上,我已经注意到,listChallenges并不包含所有对象,而selectedCategoriesId很好。正因为如此,uccors才是问题所在