How to delete a document from the firestore cloud and the corresponding item in recyclerview?

37 Views Asked by At

I use the next code to delete displayed documents in recyclerView from cloud firestore by click on the recyclerView item. The result is when I click on the item, document is deleted only from firestore.

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = holder.getAdapterPosition();
        if (position != RecyclerView.NO_POSITION) {
            DocumentSnapshot documentSnapshot = getSnapshots().getSnapshot(position);
            String medicamentId = documentSnapshot.getId();

            collref.document(medicamentId).delete()
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            // Introduce a slight delay before notifying item removal
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    notifyItemRemoved(position);
                                }
                            }, 100); 
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {});
        }
    }
}); 

How deleted the item from both firestore and from recyclerView?

0

There are 0 best solutions below