Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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 已跳过侦听器中的Firebase侦听器_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

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

Java 已跳过侦听器中的Firebase侦听器,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,在我的应用程序中,我使用firebase数据库。在单独的节点中存储有问题和相应的注释。现在我试着让一个听众提问,让另一个听众评论。不幸的是,我被他们的行为弄糊涂了:recyclerView总是得到一个空的问题列表,就像第二个侦听器被跳过一样。但是在recyclerView获得列表并设置适配器之后,我的LogCat开始打印问题和注释信息。 但是为什么在处理数据的for循环完成之前填充并使用recyclerView 获取信息的方法: private void getQuestionsFromData

在我的应用程序中,我使用firebase数据库。在单独的节点中存储有问题和相应的注释。现在我试着让一个听众提问,让另一个听众评论。不幸的是,我被他们的行为弄糊涂了:
recyclerView
总是得到一个空的
问题列表
,就像第二个侦听器被跳过一样。但是在
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) {

        }

    });

}
private void getQuestionsFromDatabase(){
mQuestions.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
questionList=新的ArrayList();
对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){
最后一个字符串title=dataSnapshot1.child(“title”).getValue().toString();
最后一个字符串question=dataSnapshot1.child(“question”).getValue().toString();
最后一个字符串commentId=dataSnapshot1.child(“commentId”).getValue().toString();
mComments.child(commentId).addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
count=dataSnapshot.getChildrenCount();
QuestionModel QuestionModel=新的QuestionModel(标题、问题、commentId、String.valueOf(计数));
问题列表。添加(问题模型);
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
Log.d(“问题列表长度:”,String.valueOf(questionList.size());
recyclerViewAdapter=新的RecyclerViewQuestionAdapter(questionList,getActivity());
recyclerViewlayoutManager=新的LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
设置适配器(RecycleWebAdapter);
}
@凌驾
已取消的公共void(@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) {

                }
            });

        }

啊,好吧,听起来合乎逻辑!我也考虑过这个解决方案,但它并不是一次列出所有结果,而是在加载时一个接一个地列出。你有办法避免这种情况吗?也尝试使用
addValueEventListener
而不是
addListenerForSingleValueEvent
你能解释一下progressbar的用法吗?在这一点上,我知道数据已被检索,当我最后没有显示结果,但总是更新结果时,如何防止显示结果?好的,那么在这里使用
SingleValueEvent
是否不正确?您能解释一下为什么这是获取数据的更好方法吗?因为addvalueeventlistener将继续侦听数据库中的更改,因此它将能够一次检索所有数据