0

I am Trying To Populate An ArrayList From FireBase Database. The Size of ArrayList Inside Event Listener is Actually Equal to some Number But Outside the Listener Value is Zero.

The Value Of Size of questions Array Should be greater than zero As I am adding Elements in It. Please Help me to identify the problem. As I am constantly trying for hours to resolve this issue.

ArrayList <Question> questions = new ArrayList<Question>();
int totalQuestCount =0;
FirebaseDatabase fDataBase;
DatabaseReference quizRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz_interface);

    Log.d("QUESTION_SIZE1", String.valueOf(questions.size()));
    fDataBase = FirebaseDatabase.getInstance();
    quizRef = fDataBase.getReference("quizes/quiz1");
    quizRef.keepSynced(true);
    quizRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists() && dataSnapshot.hasChildren()){
                totalQuestCount=(int)dataSnapshot.getChildrenCount();
                questions.ensureCapacity(totalQuestCount);
                Log.d("QuestionCount", String.valueOf(totalQuestCount));
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    questions.add(data.getValue(Question.class));
                }
                Log.d("QUESTION_SIZE2", String.valueOf(questions.size()));
            }else{
                Log.d("DATASNAP", "DOES NOT EXISTS");
            }
        }

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

        }

    });
    Log.d("QUESTION_SIZE3", String.valueOf(questions.size()));


}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Moaz Sarwar
  • 83
  • 1
  • 4
  • It's not so much about accessing `questions` outside of `onDataChange` as it is about the fact that **when** you log `questions.size()`, the `onDataChange` hasn't run yet. All code that needs the data from the database, needs to be (called from) inside `onDataChange`. See https://stackoverflow.com/questions/46396846/how-can-i-get-data-out-of-ondatachange-method and https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Oct 06 '18 at 14:57

1 Answers1

1

In this block

 for(DataSnapshot data: dataSnapshot.getChildren()){
                questions.add(data.getValue(Question.class));
    }

put

System.out.println(data.getValue(Question.class));

and check if actually you are getting data from firebase, and adding it.?

like,

 for(DataSnapshot data: dataSnapshot.getChildren()){
                System.out.println(data.getValue(Question.class));
                questions.add(data.getValue(Question.class));
    }

then let us know.

Samin
  • 577
  • 6
  • 6
  • Yes! Its Getting Data From Firebase. The Data is available for use in OnDataChange() But There Is no Data Outside the function – Moaz Sarwar Oct 06 '18 at 13:26