获取所有字段名称形成一个arylylist的firestore文档

时间:2018-06-05 13:32:57

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

我正在尝试使用文档中的所有字段名称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());
                    }
                }
            });

enter image description here

谢谢:D

1 个答案:

答案 0 :(得分:3)

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

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