简体   繁体   中英

Recyclerview not updating after item was deleting

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();
    }
};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM