简体   繁体   English

在适配器类中刷新/重置recyclerview适配器

[英]Refresh / reset recyclerview adapter inside adapter class

I am using the sqlite database to retrieve the data through adapter, and in onBindViewHolder I am making changes in database via on click method. 我正在使用sqlite数据库通过适配器检索数据,并且在onBindViewHolder我通过on click方法对数据库进行了更改。

I need to refresh the recycler view adapter inside RecyclerView.Adapter<RecyclerView.ViewHolder> class to reflect the changes. 我需要刷新RecyclerView.Adapter<RecyclerView.ViewHolder>类中的recycler视图适配器以反映更改。

I have tried the notifyDataSetChanged(); 我已经尝试了notifyDataSetChanged(); at the end of on click listener method but it's not working. 在on click listener方法的末尾,但它不起作用。 Here is the code snippet of onBindViewHolder : 这是onBindViewHolder的代码片段:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    ...

    // set the on click listener to checkBoxTaskDone
    ((VHItem) holder).checkBoxTaskDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ContentValues contentValues = new ContentValues();

            if (((VHItem) holder).checkBoxTaskDone.isChecked()) {

                ((VHItem) holder).checkBoxTaskDone.setChecked(true);

                contentValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, description);
                contentValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, priority + 4);
                contentValues.put(TaskContract.TaskEntry.COLUMN_CHECKBOX, 1);

                Uri newUri = mContext.getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI, contentValues);

                if (newUri == null) {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "new completed task creation failed",
                            Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "new completed task created",
                            Toast.LENGTH_SHORT).show();
                }

                Uri currentItemUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, id);

                int rowsDeleted = mContext.getContentResolver().delete(currentItemUri, null, null);

                if (rowsDeleted == 0) {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "task deletion failed",
                            Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "task deleted",
                            Toast.LENGTH_SHORT).show();
                }

            } else {

                ((VHItem) holder).checkBoxTaskDone.setChecked(false);

                Uri currentItemUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, id);

                int rowsDeleted = mContext.getContentResolver().delete(currentItemUri, null, null);

                if (rowsDeleted == 0) {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "completed task deletion failed",
                            Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "completed task deleted",
                            Toast.LENGTH_SHORT).show();
                }

                contentValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, description);
                contentValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, priority - 4);
                contentValues.put(TaskContract.TaskEntry.COLUMN_CHECKBOX, 0);

                Uri newUri = mContext.getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI, contentValues);

                if (newUri == null) {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "task incompletion failed",
                            Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(CustomCursorAdapter.this.mContext, "task uncompleted",
                            Toast.LENGTH_SHORT).show();
                }
            }
            notifyDataSetChanged(); \\not working
        }
    });

    ...
}

if you are changing data on click listener then use notifyItemChanged instead of notifyDataSetChanged() notifyItemChanged will fresh single updated view instead of whole list. 如果要在单击侦听器上更改数据,则使用notifyItemChanged而不是notifyDataSetChanged() notifyItemChanged将刷新单个更新的视图,而不是整个列表。

public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshView(position);
            }
    });
}

public void refreshView(int position) {
    notifyItemChanged(position);
}

Figured out myself that the problem was not with the adapter but to refresh the cursor itself to reflect the updated list of tasks from database. 我自己发现问题不是出在适配器上,而是刷新游标本身以反映数据库中更新的任务列表。 Achieved this by restarting the loader defined in Main Activity to return cursor with data from database. 通过重新启动Main Activity中定义的加载器以从数据库返回带有数据的游标来实现此目的。

Triggered the restart loader method in main activity through adapter by shared click method defined below. 通过以下定义的共享单击方法,通过适配器在主要活动中触发了重新启动加载器方法。

Callback from Adapter 从适配器回调

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

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