我正在使用Android Studio连接Firestore和Android应用。如何在侦听器调用后将数据存储在侦听器中以使用,但始终显示null值。似乎addOnCompleteListener
进入了后台,documentReference
变量首先使用了如何解决它。
和
documentReference变量显示空值
DocumentReference documentReference;
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("CustomerUser")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
documentReference = document.getDocumentReference("businessUsedID");
Log.d("REF_DATA", document.getId() + " => " + documentReference.getId());
}
} else {
Log.w("REF_DATA", "Error getting documents.", task.getException());
}
}
});
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
Log.i("REF_DATA", "onEvent: "+documentSnapshot.getString("PaymentMode"));
}
});