If you run your current code in a debugger and set some breakpoints, you'll see that Log.d("DB", String.valueOf(questions))
runs before any of the questions.add(question)
. This is because data is loaded from Firestore (and most modern cloud APIs) asynchronously.
All code that needs access to the data from the database needs to be inside the onComplete
block. So something like:
db.collection("fastmode")
.get()
.addOnCompleteListener(new OnCompleteListener < QuerySnapshot > () {
@Override
public void onComplete(@NonNull Task < QuerySnapshot > task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot: task.getResult()) {
String question = documentSnapshot.getString("question");
String answer = documentSnapshot.getString("answer");
Log.d("DB", question);
Log.d("DB", answer);
questions.add(question);
}
Log.d("DB", String.valueOf(questions));
Intent in = new Intent(getApplicationContext(), FastMode.class);
startActivity( in );
}
}
});
Also see:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…