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()));
}
}