【问题标题】:How to fetch an array of data from Cloud Firestore? [duplicate]如何从 Cloud Firestore 获取数据数组? [复制]
【发布时间】:2018-07-05 03:42:48
【问题描述】:

这是我的代码:

db.getFirestore().document("state/Madhya Pradesh/cities").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            DocumentSnapshot documentSnapshot=task.getResult();
        }
    });

【问题讨论】:

  • 请检查副本以了解如何从 Cloud Firestore 数据库中获取数组。

标签: java android firebase google-cloud-firestore


【解决方案1】:

为了从 firestore 获取数组,请执行以下操作:-

DocumentReference documentReference = firestore.collection("states").document(uid);

documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot){
        if(documentSnapshot.exists()){

            ArrayList<String> arrList = new ArrayList<String>();
            arrList = (ArrayList) documentSnapshot.get("cities");

            for(int i=0;i<arrList.size();i++){
                //Traversing the list
            }
        }
}

【讨论】:

    【解决方案2】:

    请检查以下代码。希望对你有帮助

      db.collection("Collection name").document("Document Id")
                            .collection("Collection name").get().addOnCompleteListener {
                                if (it.isSuccessful) {
                                    if (it.result.isEmpty) {
    
                                      //List is empty
                                    } else {
                                         val result = it.result.documents.map { it.toObject(YourModelClass::class.java)!! }
                                       //Performe array list operation
                                       // result is your array list
                                    }
                                } else {
                                    it.exception?.let {
                                       //Print exception
                                    }
                                }
                            }
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多