Florian Walther
Florian Walther

Reputation: 6971

FirebaseUI FirestoreRecyclerAdapter - Get DocumentReference to delete it

What is the correct way to delete an item from the Firebase-UI RecyclerView for Firestore?

This is my approach:

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            deleteItem(viewHolder.getAdapterPosition());
        }
    }).attachToRecyclerView(recyclerView);

private void deleteItem(int position) {
    DocumentSnapshot ds = (DocumentSnapshot) adapter.getSnapshots().getSnapshot(position);
    DocumentReference document = ds.getReference();
    document.delete();
}

Why does getSnapshot return an Object and not a DocumentSnapshot? Is the cast to (DocumentSnapshot) correct?

Also is it correct that I don't have to call notifyDataSetChanged or notifyItemRemoved because the adapter already reacts to changes in the database?

Upvotes: 1

Views: 1196

Answers (2)

matshidis
matshidis

Reputation: 558

The correct method is to create a Firebase-UI-Adapter like so:

public class YourFirestoreAdapter extends 
FirestoreRecyclerAdapter<YourClass, RecyclerView.ViewHolder> {

//complete the method here for the Adapter, like you would a normal Recycle adapter.

Then after the onCreateViewHolder method add a method to delete an item like so (Within your Adapter):

public void deleteItem(int position) {
        getSnapshots().getSnapshot(position).getReference().delete();
        }

Then call this method from your activity whenever you want to delete an item, by passing the position from your ItemTouchHelper.

new ItemTouchHelper(new 
 ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder 
 viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            adapter.deleteItem(viewHolder.getAdapterPosition());
        }
    }).attachToRecyclerView(recyclerView);

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 1

According to your comment, if you want to delete an item from the database, then you should use delete() method directly on the DocumentReference object.

db.collection("YourCollection").document("DocumentToDelete")
    .delete()
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "DocumentSnapshot successfully deleted!");
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w(TAG, "Error deleting document", e);
        }
    });

Because you are using Firebase-UI, this change will be automatically seen, without the use of this method call:

notifyDataSetChanged();

Please also note that deleting a document does not delete its subcollections, if you have some.

Upvotes: 1

Related Questions