0

I have a similiar problem as mentoned here enter link description here

but the suggested solution does neither working for me. My recyclerview list items from an sqlite db, when i swipped to the left, the corresponding data is deleted successfully from the db. For some reason yesterday, everything worked perfectly and the deleted item disappear from the list, but since today the item is still visible in the recyclerview. Here is my code:

 ItemTouchHelper.SimpleCallback itemTouchHelperCallback = 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) {
        AlertDialog deleteFileDialog = new AlertDialog.Builder(DayListActivity.this)
                .setTitle()
                .setMessage()
                .setPositiveButton(ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        removeDay((long)viewHolder.itemView.getTag());
                        removeRecords((long)viewHolder.itemView.getTag());
                        getAllDays();//try to refresh recyclerview by calling function to 
                                     //load data from db
                        dayListAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
                        dayListAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(R.string.file_delete_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        deleteFileDialog.show();
    }
};
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • I think dayListADapter.notifyItemRemoved will be working fine but there can be errors in removeDay() function and removeRecards() functions, or share the removeDay() and removeRecords() functions here. – Win Phyoe Thu Dec 19 '19 at 08:31

1 Answers1

0

First of all, does "getAllDays()" returns the correct data from db when you delete one? And does it set the new data from db into your adapter?

Second, if it does so, you should not call notifyItemRemoved and notifyDataSetChanged, because when you set your new data you should already call this.

Third, if it does not, you dont have to call both methods. If you changed all data, than use notifyDataSetChanged to bind all data again (not recommended). It usually should be enough in this case to say notifyItemRemoved

thehrlein
  • 133
  • 1
  • 4
  • Sorry, forget to mentioned. getAllDays returns all avaiable days from the database and pass them to the recyclerview adapter. The method is called always if the activity starts. But i do not understand why yesterday seems everything works perfectly and today is does not? –  Dec 19 '19 at 09:17
  • No i deleted this 2 lines: dayListAdapter.notifyItemRemoved(viewHolder.getAdapterPosition()); dayListAdapter.notifyDataSetChanged(); and keep calling getAllDays. It works again now, lets see how long –  Dec 19 '19 at 09:21