如何从Cloud Firestore获取数据数组?

时间:2018-07-05 03:42:49

标签: java android firebase google-cloud-firestore

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

为了从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
            }
        }
}

答案 1 :(得分:0)

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

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