【问题标题】:How to retrieve array map data and store in array in android studio from firestore? [closed]如何从firestore检索数组映射数据并存储在android studio中的数组中? [关闭]
【发布时间】:2021-08-17 06:39:35
【问题描述】:

我想从 firestore 检索数组映射并将其存储在一个数组中,但它似乎根本不起作用,关于如何解决它的任何想法?谢谢...


code example


Firebase database example


Full database structure


Error

DocumentReference docRef = db.collection("user").document("65pH8UOnV4Pz8oxogsqsL9eE91L2");
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
                             String[] array = (String[]) document.getData().get("card");
                                    Log.d(TAG, "DocumentSnapshot data: "+ document.getData().get("card"));
                        } else {
                            Log.d(TAG, "No such document");
                        }
                    } else {
                        Log.d(TAG, "get failed with ", task.getException());
                    }
                }
            });

【问题讨论】:

    标签: java arrays google-cloud-firestore


    【解决方案1】:

    为了解析结果,您可以制作与 firestore 上具有相同结构的模型 java 类,然后在您的 onComplete 方法中使用它来解析。

    //UserResponse.java
    
    
     public class UserResponse {
    
    private ArrayList<Card> card;
    
    public ArrayList<Card> getCard() {
        return card;
    }
    
    public void setCard(ArrayList<Card> card) {
        this.card = card;
    }
    
    public String getBalance() {
        return balance;
    }
    
    public void setBalance(String balance) {
        this.balance = balance;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getPhonenumber() {
        return phonenumber;
    }
    
    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }
    
    private String balance;
    private String email;
    private String name;
    private String phonenumber;
    
    
    }
    //Card.java
    public class Card{
    
        private String cvv;
    
        private String exp;
    
        public String getCvv() {
            return cvv;
        }
    
        public void setCvv(String cvv) {
            this.cvv = cvv;
        }
    
        public String getExp() {
            return exp;
        }
    
        public void setExp(String exp) {
            this.exp = exp;
        }
    
        public String getCarddetail() {
            return carddetail;
        }
    
        public void setCarddetail(String carddetail) {
            this.carddetail = carddetail;
        }
    
        private String carddetail;
       
    }
    

    然后在firestore中获取代码更改添加以下行

    UserResponse userResponse = document.toObject(UserResponse.class)
    ArrayList<Card> cards = userResponse.getCard();
    

    像这样:

    DocumentReference docRef = db.collection("user").document("65pH8UOnV4Pz8oxogsqsL9eE91L2");
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    UserResponse userResponse = document.toObject(UserResponse.class);
                 ArrayList<Card> cards = userResponse.getCard();
    
                    Log.d(TAG, "DocumentSnapshot data: "+ userResponse.getCard());
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
    

    【讨论】:

    • 它显示 DocumentSnapshot 数据:null 因为在类上找不到余额的设置器/字段
    • 你能分享你从日志中得到的确切错误吗?另外,您的地图或卡片文档中是否有任何余额字段。分享您尝试解析的完整文档结构。
    • 是的,请再次参考我的帖子。我已经编辑并上传了新的完整文档结构
    • 我已经更新了答案。您只需在正确的类中添加您尝试解析的所有字段,它就会起作用
    • 不工作 T.T,很抱歉,我已经上传了错误,请再次参考帖子。对此我感到非常抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2019-07-03
    • 2012-08-06
    • 1970-01-01
    • 2011-02-01
    • 2021-08-15
    相关资源
    最近更新 更多