【发布时间】:2018-04-10 13:15:24
【问题描述】:
我正在使用 sqlite 数据库通过适配器检索数据,并在onBindViewHolder 中通过单击方法对数据库进行更改。
我需要刷新 RecyclerView.Adapter<RecyclerView.ViewHolder> 类中的回收器视图适配器以反映更改。
我在点击监听方法的末尾尝试了notifyDataSetChanged();,但它不起作用。这里是onBindViewHolder的代码sn-p:
@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
}
});
...
}
【问题讨论】:
-
然后在通知数据集更改的项目更改上使用它将刷新已更改的视图。!
标签: android android-recyclerview