Getting all field names form a firestore document

2019-07-14 05:31发布

I'm trying t get an ArrayList with all the field names from a document and convert it to an 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());
                    }
                }
            });

enter image description here

Thank you :D

1条回答
我只想做你的唯一
2楼-- · 2019-07-14 06:04

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
        }
    }
});

The output will be:

Clinica
FEUP
outra
查看更多
登录 后发表回答