简体   繁体   English

"如何从 android firestore 中的 QuerySnapshot 获取密钥"

[英]How to get key from QuerySnapshot in android firestore

这是我的数据库的图像。

Above is an image of my database.The three fields have other info linked to them.上面是我的数据库的图像。这三个字段具有链接到它们的其他信息。

I want to store the names of these three documents (Joke Writing, Negotiation, Psychology) in a list.我想将这三个文档的名称(笑话写作、谈判、心理学)存储在一个列表中。 Here is what I'm doing:这是我正在做的事情:

List<String> na = new ArrayList<>();
        db.collection("courses").get().addOnCompleteListener(task -> {
            for(DocumentSnapshot document:task.getResult().getDocuments()){
                    na.add((String) document.get("key"));
            }
            Log.i("TAG", String.valueOf(na));
        });

However, na is printing [null,null,null] .但是, na正在打印[null,null,null] How can I get the keys?我怎样才能得到钥匙?

Your code reads a field called key<\/code> from each document, and it looks like no such field exits.您的代码从每个文档中读取一个名为key<\/code>的字段,并且看起来没有这样的字段存在。

If you instead want to get the document ID for each document, that'd be:如果您想获取每个文档的文档 ID,那就是:

na.add(document.getId());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM