获取列表中文档中的所有字段 - Firestore Java

Mal*_*yum 3 java android firebase google-cloud-firestore

我正在尝试将文档中的所有字段获取到ListView. 我试过 foreach 循环,但它不起作用。

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            // Get all fields to a list
        }
    });
Run Code Online (Sandbox Code Playgroud)

Ale*_*amo 9

要将文档中所有属性的所有值添加到列表中,请使用以下代码行:

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<String> list = new ArrayList<>();

                Map<String, Object> map = document.getData();
                if (map != null) {
                    for (Map.Entry<String, Object> entry : map.entrySet()) {
                        list.add(entry.getValue().toString());
                    }
                }

                //So what you need to do with your list
                for (String s : list) {
                    Log.d("TAG", s);
                }
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)