【问题标题】:Firestore : after getting data from firestore and get the values from object and add it to listviewFirestore:从firestore获取数据并从对象获取值并将其添加到listview
【发布时间】:2018-10-14 12:43:44
【问题描述】:

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

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

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) 中的金额、电话号码和状态并添加到列表视图中。 我无法首先获得这些字段。之后,我需要将它们添加到列表视图。而且每当用户更新字段状态时,列表视图也应该更新。

【问题讨论】:

标签: java android firebase google-cloud-firestore


【解决方案1】:

假设您的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

【讨论】:

  • 我需要从文档中的所有邀请中获取。我需要一个循环并将其存储在变量中并将其添加到列表视图中
  • 我知道documentId不需要使用QuerySnapshot
  • 如果您在代码中使用引用,您可以获得一个文档,即在您的引用 .document(documentId) 中使用的具有特定 ID 的文档。您无法使用该引用循环浏览其他文档。 唯一 您可以在所有文档中获取数据的方式与我更新的答案相同。所以使用QueryDocumentSnapshot 是必须的,对吧?
  • 我不想循环其他文档。在一个文档中我有很多邀请我想循环这些邀请并获取值
  • 您没有很清楚地解释问题。要快速解决,请添加您的读取数据库结构并指出您想要获得的确切值。截图就够了。
【解决方案2】:

您可以像下面一样实现上述目标,然后根据您的 cmets 获得您的文档 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;
    }
}

【讨论】:

  • 我没听懂你的回答
  • 我在该文档中只有一个文档需要获取值
  • 您需要使用 QuerySnapshot 从 Firestore 获取数据并将其转换为您的模型类。
  • 为什么需要使用QuerySnapshot 我知道文档id
  • 如果您有文档 ID,那么您可以获得单个对象,例如 AutoCheckIn checkIn = document.toObject(AutoCheckIn.class).withId(document.getId());
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2020-04-03
相关资源
最近更新 更多