【问题标题】:How to get data out of QuerySnapshot in Cloud Firestore如何从 Cloud Firestore 中的 QuerySnapshot 中获取数据
【发布时间】:2021-09-26 08:20:26
【问题描述】:

这个问题已经被问过很多次了,但在 Android Java 中却没有。我已经浏览了 firestore docs,它使用 for 循环来获取数据。就我而言,我的查询需要一个字段(我知道我的查询将只返回一个文档)。

这里是查询:

db.collection("songs").whereEqualTo("email",userauth.getEmail()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>(){
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                QuerySnapshot snapshot = task.getResult();
                if (!snapshot.isEmpty()) {
             //Display the data this is where I'm facing issue
}
}

我想在我的数据中显示单个字段 link。 我试过这个:

DocumentSnapshot document = task.getResult();
                    userinfo.setText(document.get("link"));

但是有一些错误。有这么多不同的快照,我不知道是做什么的。

【问题讨论】:

    标签: android google-cloud-firestore


    【解决方案1】:

    问题是查询快照总是返回一个列表,在我的例子中它将是一个只有一个对象的列表。

    所以:

    DocumentSnapshot document = snapshot.getDocuments().get(0);
                        userinfo.setText((String)document.get("link"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-17
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多