如何在Firebase Cloud Firestore Android中插入列表数据

时间:2019-10-21 07:30:59

标签: android firebase arraylist google-cloud-firestore retrofit2

我想在Firestore数据库中插入数据列表,以我为例,我仅获得单个文档条目,并且在循环内部仅更新条目,这是我的代码:

 ApiInterface apiInterface = RetrofitManager.getInstance().create(ApiInterface.class);
    Call<DoctorsResponse> call = apiInterface.getDoctors();
    call.enqueue(new Callback<DoctorsResponse>() {
        @Override
        public void onResponse(@NonNull Call<DoctorsResponse> call, @NonNull Response<DoctorsResponse> response) {
            progressDialog.setTitle("Uploading...");
            progressDialog.show();


            final String docId = FirebaseFirestore.getInstance().collection("DoctorList").document().getId();


            if (response.isSuccessful()){
                Log.e("@@size", String.valueOf(response.body().getData().size()));
                docDetailsList.addAll(response.body().getData());

                Datum datum = new Datum();
                for (int i = 0; i<docDetailsList.size(); i++){
                    String docDob = docDetailsList.get(i).getFullName();
                    datum.setFullName(docDetailsList.get(i).getFullName());
                    Log.e("@@Docdob", docDob);

                }

                FirebaseFirestore.getInstance().collection("DoctorList").document(docId).set(datum).addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {

                        progressDialog.dismiss();

                    } else {
                        Toast.makeText(MembersLists.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
        @Override
        public void onFailure(@NonNull Call<DoctorsResponse> call, @NonNull Throwable t) {

            Log.e("Error","error ");
        }
    });

在上面的代码中,我尝试了许多更改,例如每次我仅获得一个条目并且从后端获得所有响应时都将firebase查询放入循环内,请帮助我,这是我的优先事项,在此先感谢您,我非常感谢响应。

0 个答案:

没有答案