【问题标题】:Getting all field names form a firestore document to an arraylist将所有字段名称从firestore文档获取到arraylist
【发布时间】:2025-12-23 21:25:11
【问题描述】:

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

我已经能够从我将所有文档放在ArrayList 中的集合中执行此操作,但我无法从文档中执行此操作。

以下是集合中所有文档的代码和数据库的图像以及我想要的。

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

【问题讨论】:

    标签: java android firebase arraylist google-cloud-firestore


    【解决方案1】:

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

    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
    

    【讨论】: