[英]Refresh / reset recyclerview adapter inside adapter class
我正在使用sqlite数据库通过适配器检索数据,并且在onBindViewHolder
我通过on click方法对数据库进行了更改。
我需要刷新RecyclerView.Adapter<RecyclerView.ViewHolder>
类中的recycler视图适配器以反映更改。
我已经尝试了notifyDataSetChanged();
在on click listener方法的末尾,但它不起作用。 这是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
}
});
...
}
如果要在单击侦听器上更改数据,则使用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);
}
我自己发现问题不是出在适配器上,而是刷新游标本身以反映数据库中更新的任务列表。 通过重新启动Main Activity中定义的加载器以从数据库返回带有数据的游标来实现此目的。
通过以下定义的共享单击方法,通过适配器在主要活动中触发了重新启动加载器方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.