繁体   English   中英

有没有办法从Firestore获取对象的值?

[英]Is there any way to obtain the values of Objects from Firestore?

这是我数据的结构: Imgur

我试图获得allergyId1和allergyId2的值。 有没有办法分别获得“过敏原”和“描述”的价值? 我想将这些值分配给另一个名为Allergy的类,并将它们添加到ArrayList中。 例如:

Allergy allergy = new Allergy();
ArrayList<Allergy> allergies = new ArrayList<>();

// Data of "allergyId1"
allergy.setAllergen(the value of allergen);
allergy.setDescription(the value of description);

allergies.add(allergy);

我试图将返回的对象转换为Json对象,但我认为必须有一种更有效的方法从Firestore获取数据。

private void retrieveAllergiesData() {
        FirebaseUser user = mAuth.getCurrentUser();
        DocumentReference ref = db.collection(user.getUid()).document("allergies");

        ref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Map<String, Object> tmp = new HashMap<>();
                        tmp = document.getData();

                        for (Map.Entry<String, Object> entry : tmp.entrySet()) {
                            Log.d("allergy", entry.getKey() + ":" + entry.getValue().toString());
                        }

                    } else {
                        Log.d("fb", "No such document");
                    }
                } else {
                    Log.d("fb", "get failed with ", task.getException());
                }
            }
        });

    }

这就是我得到的 Imgur

这就是它的工作原理。 它在性能方面没有比处理文档返回的Map更有效。

但是,您不必要地创建新地图而不使用它:

Map<String, Object> tmp = new HashMap<>();
tmp = document.getData();

它可以像这样简化:

Map<String, Object> tmp = document.getData();

使用构造函数创建类Allergyget set

private class Allergy  {

        private String allergen;
        private String description;

        public Allergies() {

        }

        public Allergies(String allergen, String description) {
            this.allergen = allergen;
            this.description = description;
        }

        public String getAllergen() {
            return allergen;
        }

        public void setAllergen(String allergen) {
            this.allergen = allergen;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

然后用Allergy替换Object 这可能适用于您的情况。

private void retrieveAllergiesData() {
        FirebaseUser user = mAuth.getCurrentUser();
        DocumentReference ref = db.collection(user.getUid()).document("allergies");

        ref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Map<String, Allergy> tmp = new HashMap<>();
                        tmp = document.getData();

                        for (Map.Entry<String, Allergy> entry : tmp.entrySet()) {
                            String allergen = entry.getValue().getAllergen();
                            String description = entry.getValue().getDescription();
                        }

                    } else {
                        Log.d("fb", "No such document");
                    }
                } else {
                    Log.d("fb", "get failed with ", task.getException());
                }
            }
        });

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM