I swipe to dismiss in adapter but I cannot delete in Firebase

时间:2018-02-03 08:37:37

标签: java android firebase google-cloud-firestore itemtouchhelper

Without swiping item, I delete with this method:

public void deleteItem(int index) {
  Toast.makeText(this, ""+ index, Toast.LENGTH_SHORT).show();
    firebaseFirestore.collection("ToDoList").document(mAuth.getCurrentUser().getUid())
            .collection("todo")
            .document(toDoList.get(index).getId())
            .delete()
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    loadToDoList();

                    Snackbar snackbar = Snackbar
                            .make(relativeLayout, R.string.deleteIsSuccess, Snackbar.LENGTH_LONG);

                    snackbar.show();
                }
            });
}

But I wanted to use swipe to delete and I have swipehelper class to dismiss item from adapter. And when I load activity, dismiss item is showed in RecyclerWiew

public class SwiperHelper extends ItemTouchHelper.SimpleCallback {
ListAdapter adapter;
MainActivity activity;

public SwiperHelper(int dragDirs, int swipeDirs) {
    super(dragDirs, swipeDirs);
}

public SwiperHelper (ListAdapter adapter) {
    super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) ;
    this.adapter = adapter;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    adapter.dismiss(viewHolder.getAdapterPosition());

}}

I try to write my deleteItem method in onSwiped method but I can not get item database Id to delete.

1 个答案:

答案 0 :(得分:0)

只需尝试

 getSnapshot(position).getReference().delete();

使用您的deleteItem(int position)方法。

确保deleteItem方法在适配器类中。