繁体   English   中英

已跳过侦听器中的 Firebase 侦听器

[英]Firebase listener in listener skipped

在我的应用程序中,我使用了 firebase 数据库。 有问题和相应的评论存储在单独的节点中。 现在,我尝试与一位听众一起提问,并与第二位听众一起访问评论。 不幸的是,我对他们的行为感到困惑: recyclerView总是得到一个空的questionsList ,就像跳过第二个侦听器一样。 但是在recyclerView得到列表并设置了适配器之后,我的LogCat 开始打印问题和评论信息。 但是为什么在处理数据的 for 循环完成之前填充和使用recyclerView呢?

获取信息的方法:

private void getQuestionsFromDatabase() {

    mQuestions.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            questionList = new ArrayList<>();
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                final String title = dataSnapshot1.child("title").getValue().toString();
                final String question = dataSnapshot1.child("question").getValue().toString();
                final String commentId = dataSnapshot1.child("commentId").getValue().toString();

                mComments.child(commentId).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        count = dataSnapshot.getChildrenCount();

                        QuestionModel questionModel = new QuestionModel(title, question, commentId, String.valueOf(count));
                        questionList.add(questionModel);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }

            Log.d("questionList length: ", String.valueOf(questionList.size()));
            recyclerViewAdapter = new RecyclerViewQuestionAdapter(questionList, getActivity());
            recyclerViewlayoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(recyclerViewlayoutManager);
            recyclerView.setAdapter(recyclerViewAdapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });

}

之前用过,因为onDataChange是异步的,这意味着编译器不会等到从数据库中获取数据,而是会在监听器之后执行代码。 因此,要解决您的问题,您应该执行以下操作:

                mComments.child(commentId).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    count = dataSnapshot.getChildrenCount();

                    QuestionModel questionModel = new QuestionModel(title, question, commentId, String.valueOf(count));
                    questionList.add(questionModel);
                    Log.d("questionList length: ", String.valueOf(questionList.size()));
                    recyclerViewAdapter = new RecyclerViewQuestionAdapter(questionList, getActivity());
                    recyclerViewlayoutManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(recyclerViewlayoutManager);
                    recyclerView.setAdapter(recyclerViewAdapter);

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM