In this portion of code I delete a selection of items from my Firebase Realtime Database:
for (Expense i : selectedExpenses) {
final Expense expenseBackUp = new Expense(i.getItem(), i.getCategory(), i.getDate(), i.getId(), i.getAmount());
databaseReference.child(i.getId()).removeValue().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Snackbar.make(activity.findViewById(android.R.id.content), activity.getString(R.string.deleted_alert),
Snackbar.LENGTH_LONG).
setAction(R.string.undo, view1 -> databaseReference.child(i.getId()).setValue(expenseBackUp)).show();
} else {
Toast.makeText(activity.getApplicationContext(), activity.getString(R.string.input_error) + task.getException(), Toast.LENGTH_SHORT).show();
}
});
}
Now I want to implement an undo button within a Snackbar.
I get to show the Snackbar with the undo button, but I need some help with the expenseBackUp
because it only retrieves my last deleted item, which works fine when a user deletes only 1 item.
How can I go around this, so I can get every deleted item back if undo button is hit?