1. Are you ready for the Galaxy S20? Here is everything we know so far!

Delete and undo in RecyclerView causes multiple entries

Discussion in 'Android Development' started by Lalox3, Apr 26, 2019.

  1. Lalox3

    Lalox3 Lurker
    Thread Starter

    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
    Code (Java):
    1.     // Delete and undo pocket items
    2. ItemTouchHelper.SimpleCallback recyclerviewSwipe = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
    3.     @Override
    4.     public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    5.         return false;
    6.     }
    7.  
    8.     @Override
    9.     public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
    10.         final Pocket pocketList = PocketItems.get(viewHolder.getAdapterPosition());
    11.         final int deletetIndex = viewHolder.getAdapterPosition();
    12.  
    13.         pocketItemAdapter.removeItem(viewHolder.getAdapterPosition());
    14.  
    15.  
    16.         if(pocketItemAdapter.getItemCount() != 0){
    17.             textView_placeholder_recyclerview.setVisibility(View.GONE);
    18.             imageView_placeholder_recyclerview.setVisibility(View.GONE);
    19.         }else{
    20.             textView_placeholder_recyclerview.setVisibility(View.VISIBLE);
    21.             imageView_placeholder_recyclerview.setVisibility(View.VISIBLE);
    22.         }
    23.  
    24.         Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,R.string.successfully_deleted,3000)
    25.                 .setAction(R.string.undo, new View.OnClickListener() {
    26.                     @Override
    27.                     public void onClick(View view) {
    28.                         pocketItemAdapter.restoreItem(pocketList,deletetIndex);
    29.                         database.createpocketEntry(pocketList.getTIMESTAMP(),pocketList.getAMOUNT(),pocketList.getDESCRIPTION(),pocketList.getCATEGORY());
    30.                     }
    31.                 });
    32.         snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.colorBright));
    33.  
    34.         snackbar.show();
    35.     }
    RecyclerView Adapter

    Code (Java):
    1. void removeItem(int position){
    2.     Database database = new Database(context);
    3.     database.deletepocketEntry(Integer.parseInt(pocketItems.get(position).getROWID()));
    4.     pocketItems.remove(position);
    5.     notifyItemRemoved(position);
    6.  
    7.  
    8. }
    9.  
    10. void restoreItem(Pocket item, int position){
    11.     pocketItems.add(position,item);
    12.     notifyItemInserted(position);
    13.     Log.d("RESTORED","restoreItem()");
    14. }
    SQLite open helper
    Code (Java):
    1.   Integer deletepocketEntry(Integer rowID){
    2.         SQLiteDatabase database = this.getWritableDatabase();
    3.         Log.d("RESTORED","deletePocketEntry");
    4.         return database.delete(KEY_DATABASE_TABLE,"rowid = ?", new String[]{Integer.toString(rowID)});
    5. }
    6.  
    7. long createpocketEntry(String timestamp,String amount,String description, String category){
    8.     SQLiteDatabase database = this.getWritableDatabase();
    9.     ContentValues pocketValues = new ContentValues();
    10.     pocketValues.put(KEY_TIMESTAMP,timestamp);
    11.     pocketValues.put(KEY_AMOUNT,amount);
    12.     pocketValues.put(KEY_DESCRIPTION,description);
    13.     pocketValues.put(KEY_CATEGORY,category);
    14.     Log.d("RESTORED","createPocketEntry");
    15.     return database.insert(KEY_DATABASE_TABLE,null,pocketValues);
    16. }
     


  2. Lalox3

    Lalox3 Lurker
    Thread Starter




    So basicly I changed something.
    This should return 1 but it doesn't and I think this is the problem.
    Code (Java):
    1.     int removeItem(int position){
    2.         Database database = new Database(context);
    3.         int boo = database.deletepocketEntry(Integer.parseInt(pocketItems.get(position).getROWID()));
    4.         pocketItems.remove(position);
    5.         notifyItemRemoved(position);
    6.         return boo;
    7.     }
    And deletePocketEntry is:
    Code (Java):
    1.     Integer deletepocketEntry(Integer rowID){
    2.         SQLiteDatabase database = this.getWritableDatabase();
    3.         return database.delete(KEY_DATABASE_TABLE,"rowid = ?", new String[]{Integer.toString(rowID)});
    4.     }
    So everytime the red snackbar appears it's because of this because it return 0 instead of 1:
    Code (Java):
    1. if(pocketItemAdapter.removeItem(viewHolder.getAdapterPosition()) == 1){
    2.                      
    3.  
    4.                         Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,R.string.successfully_deleted,3000)
    5.                                 .setAction(R.string.undo, new View.OnClickListener() {
    6.                                     @Override
    7.                                     public void onClick(View view) {
    8.                                         pocketItemAdapter.restoreItem(pocketList,deletetIndex);
    9.                                         database.createpocketEntry(pocketList.getTIMESTAMP(),pocketList.getAMOUNT(),pocketList.getDESCRIPTION(),pocketList.getCATEGORY());
    10.                                     }
    11.                                 });
    12.                         snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.colorBright));
    13.  
    14.                         snackbar.show();
    15.                     }else{
    16.  
    17.                         final Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,"Löschen war nicht erfolgreich",Snackbar.LENGTH_SHORT);
    18.                                 snackbar.setAction("Ok", new View.OnClickListener() {
    19.                                     @Override
    20.                                     public void onClick(View view) {
    21.                                         snackbar.dismiss();
    22.                                     }
    23.                                 });
    24.                         snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.red));
    25.                         snackbar.show();
    26.                         pocketItemAdapter.restoreItem(pocketList,deletetIndex);
    27.  
    28.                     }
    29.  
     
  3. Deleted User

    Deleted User Guest

    Time to get into debugging your app because this is a difficult one to answer just by static analysis of your code.
    In particular I'd be interested to know what's happening in restoreItem, and the objects stored in pocketItems.
    So run your app in debug mode, and trace through what's happening by setting breakpoints at crucial points in your code.
     
  4. Lalox3

    Lalox3 Lurker
    Thread Starter

    Okay let me figure out a bit how debbuging works ^^
     
  5. Lalox3

    Lalox3 Lurker
    Thread Starter

    Ehm at the breakpoint at restoreItem it looks like this:looks fine for me it was 3 items before
    [​IMG]
     
    #5 Lalox3, Apr 26, 2019
    Last edited: Apr 26, 2019
  6. Deleted User

    Deleted User Guest

    Sorry I just can't read that screenshot.
     
  7. Lalox3

    Lalox3 Lurker
    Thread Starter

    No you should ^^ sorry
     
Loading...

Share This Page

Loading...