Muhammed Ovais
Muhammed Ovais

Reputation: 71

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.

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
        }
    });

    ...
}

Upvotes: 0

Views: 7057

Answers (2)

Muhammed Ovais
Muhammed Ovais

Reputation: 71

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.

Triggered the restart loader method in main activity through adapter by shared click method defined below.

Callback from Adapter

Upvotes: 0

Atif AbbAsi
Atif AbbAsi

Reputation: 6035

if you are changing data on click listener then use notifyItemChanged instead of notifyDataSetChanged() notifyItemChanged will fresh single updated view instead of whole list.

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);
}

Upvotes: 1

Related Questions