I use docRef.get()
to get a Map of a collection from the Cloud Firestore. However, I cannot use the value in the map after I leave public void onSuccess(DocumentSnapshot documentSnapshot)
method. In JS, I know I can do a subscribe
method but in Android I don't know what to do.
This is my code to get the Map:
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
doc = documentSnapshot.getData();
Toast.makeText(ChatActivity.this,"Data loaded",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ChatActivity.this,"No data exists",Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ChatActivity.this, "Error", Toast.LENGTH_SHORT).show();
Log.d("TAG",e.toString());
}
});
And I want to use the Map after that, to set the textView
to be the message
:
for(String key : doc.keySet()){
message += key + "\n" + doc.get(key)+"\n";
}
ui.setText(message);