1

I've searched a lot on internet but i could not find the exact answer i need.

After implementing this method to get data from firestore from a specific document where username="dotcom"

  public void UserData(){
        db.setFirestoreSettings(settings);
        CollectionReference peopleRef = db.collection("member");
        peopleRef.whereEqualTo("Username", "dotcom")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    DocumentSnapshot document;
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (DocumentSnapshot document : task.getResult()) {
                                Log.d("TAG", document.getId() + " => " + document.getData());

                            }

                        } else {
                            Log.d("TAG", "Error getting documents: ", task.getException());
                        }
                    }

                });
    }

i need the value of document.getData() to use it in another method inside the same class. I've tested local and global variables with setters and getters, it won't work for me :(

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
choubari
  • 41
  • 7

1 Answers1

0

As clarified in this other post from the Community - accessible here - there are a couple of actions that you might take to achieve this goal.

To summarize, all of them involve you getting your data and setting it to an ArrayList that you can manipulate and use it outside the method. Within this ArrayList, there are two possibilities as well: the use of the asynchronous method onComplete() - more information in the official documentation here - where you will be able to get the data once it's completed; and the other way is to use the toObject() to add the objects to the array.

Both of them have some benefits in relation to the other - for example, using the toObject() you will have better management over the methods while they are being called and the other onComplete() will require less implementation.

Anyway, I would recommend you to take a look at the Community post to get more information and code samples for your own usage.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • thanks for sharing the stackoverflow link, it's very helpful. I've applied List list = new ArrayList<>(); before the for loop and list.add(new Membre(document.getData())); inside the for loop, and when i print the value of list i got [com.choubapp.app.Myclass@ae3ccd4] ? i have no idea how will i work with list – choubari Mar 27 '20 at 20:58
  • Hi @choub to use `ArrayList`, I would recommend you to take a look at the official Android documentation [here](https://developer.android.com/reference/java/util/ArrayList), on how to work with lists. – gso_gabriel Mar 30 '20 at 07:30