Par*_*dox 11 android firebase google-cloud-firestore
如果我有一个Firebase Firestore数据库,我已经DocumentSnapshot
为右边的集合对应的文档检索了一个并存储在document
变量中,那么我怎样才能在DocumentSnapshot
"用户名"字段中检索该值?该字段具有字符串值.
Dou*_*son 19
DocumentSnapshot有一个方法getString(),它接受一个字段的名称并将其值作为String返回.
String value = document.getString("username");
Run Code Online (Sandbox Code Playgroud)
Ali*_*Ali 12
您可以使用get
方法来获取字段的值
String username = (String) document.get("username"); //if the field is String
Boolean b = (Boolean) document.get("isPublic"); //if the field is Boolean
Integer i = (Integer) document.get("age") //if the field is Integer
Run Code Online (Sandbox Code Playgroud)
查看文档 DocumentSnapshot
您需要执行 aDocumentReference
才能获取文档中的内容。
一个简单的会是这样的。
DocumentReference docRef = myDB.collection("users").document("username");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.i("LOGGER","First "+document.getString("first"));
Log.i("LOGGER","Last "+document.getString("last"));
Log.i("LOGGER","Born "+document.getString("born"));
} else {
Log.d("LOGGER", "No such document");
}
} else {
Log.d("LOGGER", "get failed with ", task.getException());
}
}
});
Run Code Online (Sandbox Code Playgroud)
缺点是您需要知道您的文档 ID 才能获取字段值。
归档时间: |
|
查看次数: |
18460 次 |
最近记录: |