I need to use the existing elements in the firebase database. I took them over through CollectionReference.
If I want to use doctorsList outside of the loop, it returns null. How can I fix this? How can I call the list to show me all the values added by doctorsList.add ()?
FirebaseFirestore db=FirebaseFirestore.getInstance();
ArrayList<String> doctorsList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
CollectionReference collectionReference=db.collection("users");
collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<DocumentSnapshot> myListOfDocuments = task.getResult().getDocuments();
for(DocumentSnapshot documentSnapshot:myListOfDocuments) {
CollectionReference collectionReference1 = documentSnapshot.getReference().collection("Appointment");
collectionReference1.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<DocumentSnapshot> listDocuments = task.getResult().getDocuments();
for (DocumentSnapshot documentSnapshot1 : listDocuments)
doctorsList.add(documentSnapshot1.getString("doctorName"));
// Toast.makeText(getApplicationContext(),doctorName,Toast.LENGTH_LONG).show();
//HERE DISPLAY ALL ITEMS IN THE LIST
}
});
}
Toast.makeText(getApplicationContext(),doctorsList.toString(),Toast.LENGTH_LONG).show();
// DOCTORSLIST IS EMPTY HERE
}
}
});
}