In a snackbar action, how can I be sure it's safe to permanently delete a soft-deleted record from the database?

后端 未结 5 1260
轻奢々
轻奢々 2021-02-04 02:29

I am using Snackbar in android and I have implemented an action so that user can undo the action (the action is clearing all the items in the listview).Removing and adding the i

5条回答
  •  执笔经年
    2021-02-04 02:59

    If you don't want to delete the record from database immediately, try this:

    // Backup the item for undo
    int itemIndex = viewHolder.getAdapterPosition();
    Item item = adapter.getItem(itemIndex);
    
    // Delete only from the adapter
    adapter.removeItem(itemIndex);
    
    Snackbar.make(getView(), "Item deleted", LENGTH_LONG)
            .addCallback(new BaseCallback() {
                public void onDismissed(Snackbar transientBottomBar, int event) {
                    if (event != DISMISS_EVENT_ACTION) {
                        // Dismiss wasn't because of tapping "UNDO"
                        // so here delete the item from databse
                    }
                }
            })
            .setAction("UNDO", v -> adapter.addItem(item, itemIndex))
            .show();
    

提交回复
热议问题