java - Firestore : after getting data from firestore and get the values from object and add it to listview

标签 java android firebase google-cloud-firestore

我有一个如下所示的对象数据结构:

enter image description here

这些是我检索文档数据的代码:

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 是正确的,获取 AmountPhoneNumberStatus 属性的值,请更改以下代码行:

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/

相关文章:

ios - 在表格 View 中列出附近的 GeoFire 位置

java - 错误 : ORA-02289: sequence does not exist - org. hibernate.exception.SQLGrammarException:无法提取 ResultSet

java - cvs update - 无法打开临时文件写入没有这样的文件或目录

java - Java 平台独立还是操作系统独立?

java - 如何在 Release build 的 android 中使用 Proguard 防止 LOG 打印

android - 如何正确保护使用应用内购买和本地数据库的应用程序

java - 执行domain.save()时Grails触发查找和保存查询

android - 相机获取参数失败

ios - 如何下载 Firebase 存储文件

javascript - 为什么我的 Firebase 项目无需指定 apiKey 即可运行?