have data in my SQLite Database which I load into a RecyclerView. With an ItemTouchHelper you can delete a item and a Snackbar appears with an undo .action.
The problem I have is that when I delete and undo multiple times and I reload the app, I have got multiple entries where I deleted and restored.
I have figured out that "
pocketItemAdapter.removeItem(viewHolder.getAdapterPosition())" do not return 1 so its failing. But is that a problem with Adapter or with SQLite?
As you can see here:
ItemTouchHelper
RecyclerView Adapter
SQLite open helper
The problem I have is that when I delete and undo multiple times and I reload the app, I have got multiple entries where I deleted and restored.
I have figured out that "
pocketItemAdapter.removeItem(viewHolder.getAdapterPosition())" do not return 1 so its failing. But is that a problem with Adapter or with SQLite?
As you can see here:
ItemTouchHelper
Java:
// Delete and undo pocket items
ItemTouchHelper.SimpleCallback recyclerviewSwipe = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
final Pocket pocketList = PocketItems.get(viewHolder.getAdapterPosition());
final int deletetIndex = viewHolder.getAdapterPosition();
pocketItemAdapter.removeItem(viewHolder.getAdapterPosition());
if(pocketItemAdapter.getItemCount() != 0){
textView_placeholder_recyclerview.setVisibility(View.GONE);
imageView_placeholder_recyclerview.setVisibility(View.GONE);
}else{
textView_placeholder_recyclerview.setVisibility(View.VISIBLE);
imageView_placeholder_recyclerview.setVisibility(View.VISIBLE);
}
Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,R.string.successfully_deleted,3000)
.setAction(R.string.undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
pocketItemAdapter.restoreItem(pocketList,deletetIndex);
database.createpocketEntry(pocketList.getTIMESTAMP(),pocketList.getAMOUNT(),pocketList.getDESCRIPTION(),pocketList.getCATEGORY());
}
});
snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.colorBright));
snackbar.show();
}
RecyclerView Adapter
Java:
void removeItem(int position){
Database database = new Database(context);
database.deletepocketEntry(Integer.parseInt(pocketItems.get(position).getROWID()));
pocketItems.remove(position);
notifyItemRemoved(position);
}
void restoreItem(Pocket item, int position){
pocketItems.add(position,item);
notifyItemInserted(position);
Log.d("RESTORED","restoreItem()");
}
SQLite open helper
Java:
Integer deletepocketEntry(Integer rowID){
SQLiteDatabase database = this.getWritableDatabase();
Log.d("RESTORED","deletePocketEntry");
return database.delete(KEY_DATABASE_TABLE,"rowid = ?", new String[]{Integer.toString(rowID)});
}
long createpocketEntry(String timestamp,String amount,String description, String category){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues pocketValues = new ContentValues();
pocketValues.put(KEY_TIMESTAMP,timestamp);
pocketValues.put(KEY_AMOUNT,amount);
pocketValues.put(KEY_DESCRIPTION,description);
pocketValues.put(KEY_CATEGORY,category);
Log.d("RESTORED","createPocketEntry");
return database.insert(KEY_DATABASE_TABLE,null,pocketValues);
}