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.
I need to refresh the recycler view adapter inside RecyclerView.Adapter<RecyclerView.ViewHolder>
class to reflect the changes.
I have tried the notifyDataSetChanged();
at the end of on click listener method but it's not working. Here is the code snippet of 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
}
});
...
}