我正在使用此代码:
DocumentReference docRef = db.collection("Users").document("user_01");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
但我不知道如何获取。我知道在put()
的帮助下我能够在 Cloud Firestore 上插入数据,但我不知道如何获取放置在 user_01
中的数据.
最佳答案
假设每个用户对象下都有一个名为 name
的属性,要解决此问题,请使用以下代码:
DocumentReference docRef = db.collection("Users").document("user_01");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
Log.d("TAG", document.getString("name")); //Print the name
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
关于java - 如何在android中从Cloud Firestore Firebase获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49362614/