Firestore calls the data Twice

Viewed 17

I am trying to call document and apply to recycler view, but in the recycler view, the items are double. I have checked other questions as well, but am sure its related to mine.

Here is the code

private void loadData() {
    firestore.collection("Products").orderBy("order")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {

                    if(error != null) {

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                            Log.e("Firestore gives server error", error.getMessage());
                            new Flashbar.Builder(requireActivity()).gravity(Flashbar.Gravity.BOTTOM).duration(500).backgroundColorRes(com.margsapp.iosdialog.R.color.red).messageColorRes(R.color.white).showOverlay().showIcon().message("Something went wrong please try again later.").build();
                        }
                    }
                    assert value != null;
                    for (DocumentChange documentChange : value.getDocumentChanges()){
                        if(documentChange.getType() == DocumentChange.Type.ADDED){
                         productList.add(documentChange.getDocument().toObject(Product.class));
                        }
                    }
                    productAdapter.notifyDataSetChanged();
                    if(progressDialog.isShowing()){
                        progressDialog.dismiss();
                    }
                }
            });
0 Answers
Related