【问题标题】:Refresh / reset recyclerview adapter inside adapter class在适配器类中刷新/重置 recyclerview 适配器
【发布时间】: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


【解决方案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);
}

【讨论】:

  • 列表仍然没有自行更新。它会在重新启动应用程序或设备方向更改时更新
【解决方案2】:

我自己发现问题不在于适配器,而是刷新光标本身以反映数据库中更新的任务列表。通过重新启动 Main Activity 中定义的加载器以从数据库返回游标和数据来实现这一点。

通过下面定义的共享单击方法通过适配器触发主活动中的重新启动加载器方法。

Callback from Adapter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多