简体   繁体   English

删除项目后 Recyclerview 未更新

[英]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.我的 recyclerview 列出了来自 sqlite 数据库的项目,当我向左滑动时,相应的数据已从数据库中成功删除。 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?首先,当你删除一个时,“getAllDays()”是否从db返回正确的数据? And does it set the new data from db into your adapter?它是否将 db 中的新数据设置到您的适配器中?

Second, if it does so, you should not call notifyItemRemoved and notifyDataSetChanged, because when you set your new data you should already call this.其次,如果这样做,您不应该调用notifyItemRemoved 和notifyDataSetChanged,因为当您设置新数据时,您应该已经调用了它。

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).如果您更改了所有数据,则使用 notifyDataSetChanged 再次绑定所有数据(不推荐)。 It usually should be enough in this case to say notifyItemRemoved在这种情况下,通常说 notifyItemRemoved 就足够了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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