获取所有字段名称将一个Firestore文档形成一个数组列表 [英] Getting all field names form a firestore document to an arraylist

查看:86
本文介绍了获取所有字段名称将一个Firestore文档形成一个数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文档中获取具有所有字段名称的ArrayList,并将其转换为ArrayList.

I'm trying t get an ArrayList with all the field names from a document and convert it to an ArrayList.

我已经能够通过将所有文档放入ArrayList的集合来做到这一点,但是我不能从文档中做到这一点.

I've been able to do this from a collection where I put all the documents in a ArrayList but I can't do it from a document.

下面是来自一个集合的所有文档的代码以及数据库的图像以及我想要的.

Below is the code for all the documents from a colection and an image of the data base and what I want.

names_clinics= new ArrayList<>();

    mFirebaseFirestore = FirebaseFirestore.getInstance();
    mFirebaseFirestore.collection("CodeClinic")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            names_clinics.add(document.getId());
                            Log.d("CLINIC CODE", document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d("CLINIC CODE", "Error getting documents: ", task.getException());
                    }
                }
            });

谢谢:D

推荐答案

要打印这些属性名称,请使用以下代码:

To print those property names, please use the following code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference codesRef = rootRef.collection("CodeClinic").document("Codes");
codesRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            List<String> list = new ArrayList<>();
            Map<String, Object> map = task.getResult().getData();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                list.add(entry.getKey());
                Log.d("TAG", entry.getKey());
            }
            //Do what you want to do with your list
        }
    }
});

输出将是:

Clinica
FEUP
outra

这篇关于获取所有字段名称将一个Firestore文档形成一个数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆