我有一个像下面这样的对象数据结构:
这些是我已经检索过文档数据的代码:
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也应该更新。
答案 0 :(得分:1)
您可以通过以下方式达到以上目的,根据您的意见,您可以获得文档ID
Invite invite = document.toObject(Invite.class).withId(document.getId());
public class Invite {
private int amount;
private String phoneNumber;
private String status;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
答案 1 :(得分:1)
假设您的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
<强> EDIT2:强>
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