我有一个如下所示的对象数据结构:
这些是我检索文档数据的代码:
DocumentReference docRef = db.collection("deyaPayUsers").document(mAuth.getUid()).collection("Split").document(mAuth.getUid()).collection("SentInvitations").document(documentId);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
Object value = document.getData();// Here I added the data to the object type value
System.out.println("values"+ value);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
我已从 Firestore 数据库中检索到这些数据。现在我需要所有 Invite(1,2) 中的金额、电话号码和状态,并添加到 ListView 中。 我无法首先获得这些字段。之后我需要将它们添加到 ListView 中。而且每当用户更新字段状态时, ListView 也应该更新。
最佳答案
假设您的 docRef
DocumentReference 是正确的,获取 Amount
、PhoneNumber
和 Status
属性的值,请更改以下代码行:
Object value = document.getData();// Here I added the data to the object type value
System.out.println("values"+ value);
到
String amount = document.getString("Amount");
String phoneNumber = document.getString("PhoneNumber");
String status = document.getString("Status");
System.out.println(amount + " / " + phoneNumber + " / " + status);
假设您想要获取 Invite1 文档的属性值,输出将是:
10 / 9876543210 / Pending
编辑:根据您的评论,我了解到您想从所有文档中获取这些属性的值,但在您的代码中您使用的是以下引用,它指向单个文档并且不是整个集合。
DocumentReference docRef = db
.collection("deyaPayUsers")
.document(mAuth.getUid())
.collection("Split")
.document(mAuth.getUid())
.collection("SentInvitations")
.document(documentId); //Reference to a document
看到,调用的最后一个方法是.document(documentId)
?要获取所有文档,您需要使用 CollectionReference
。所以请使用以下代码:
DocumentReference docRef = db
.collection("deyaPayUsers")
.document(mAuth.getUid())
.collection("Split")
.document(mAuth.getUid())
.collection("SentInvitations").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> map = document.getData();
String amount = map.get("Amount").toString();
String phoneNumber = map.get("PhoneNumber").toString();
String status = map.get("Status").toString();
System.out.println(amount + " / " + phoneNumber + " / " + status);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
输出将是:
10 / 9876543210 / Pending
20 / 1234566789 / Pending
编辑2:
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Map<String, Object> map = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Map<String, Object> m = (Map<String, Object>) entry.getValue();
StringBuilder s = new StringBuilder();
for (Map.Entry<String, Object> e : m.entrySet()) {
s.append(e.getValue() + " ");
}
Log.d(TAG, s.toString());
}
}
}
}
});
输出将是:
10 9876543210 Pending
20 1234566789 Pending
//And so on
关于java - Firestore : after getting data from firestore and get the values from object and add it to listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50167964/