1
 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

                }
            }
        });



    }
Antonia
  • 19
  • 1
  • The snippet of code you added shouldn't give null to doctorsList, So, are you assigning a value to doctorsList anywhere else? – Tefa Jun 27 '21 at 11:55
  • no, here are all the places where I use doctorsList. I initialized it and added values using the add method. – Antonia Jun 27 '21 at 11:57
  • weird, It shouldn't give null, maybe empty but not null – Tefa Jun 27 '21 at 11:58

2 Answers2

1

That list is empty because the code that adds entries has not run yet. What did you think onComplete meant?

It means that the code runs when collectionReference1 (please name your variables a little better) is 'complete', whatever that means. That won't be immediately, obviously - if it had been, this code wouldn't require you to 'add a completion listener', you'd just invoke getTask() on it and carry on from there.

The solution is to do whatever you want to do with doctorsList inside the onComplete code, or, alternatively, fire an event inside this code.

In general, user interfaces (and android is no different here) are glued together using an event stack. The user clicks on buttons or does other actions and this causes events to fire. The OS itself also causes events to fire ('your app is going in the background now', for example). It's all events. Events go on a pile and a single thread handles them, one at a time, by farming out the work to some registered 'event handler'. Your onComplete method is one such handler for one such event stack.

Toast.makeText is particularly simple and toast (if my memory serves) doesn't require that you are in a GUI event handler (which your onComplete is not), but almost everything else you care to do with your GUI does require it, so if you want to do a lot more to the user interface than just show a toast block, you'd have to fire an event programatically in the onComplete handler and then write an event handler for that event elsewhere.

Any half decent android GUI tutorial will cover this stuff.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
1

The ArrayList is null at the time of executing your Toast message, because your task runs asynchronously. You need to handle the data change programatically.

Sai H.
  • 66
  • 5