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

时间:2018-04-10 13:15:25

标签: android android-recyclerview

我正在使用sqlite数据库通过适配器检索数据,并在onBindViewHolder我通过点击方法在数据库中进行更改。

我需要刷新RecyclerView.Adapter<RecyclerView.ViewHolder>类中的回收器视图适配器以反映更改。

我在点击监听器方法的最后尝试了notifyDataSetChanged();,但它无法正常工作。以下是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
        }
    });

    ...
}

2 个答案:

答案 0 :(得分:1)

如果您要更改点击监听器上的数据,那么使用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);
}

答案 1 :(得分:0)

弄清楚自己问题不在于适配器,而在于刷新光标本身以反映数据库中更新的任务列表。通过重新启动Main Activity中定义的加载程序以使用数据库中的数据返回游标来实现此目的。

通过以下定义的共享点击方法,通过适配器触发主活动中的重启加载程序方法。

Callback from Adapter