从另一个 class 调用 RecyclerView.Adapter 上的 notifyItemChanged()

Calling notifyItemChanged() on a RecyclerView.Adapter from another class

我在 AdapterActivity 中有一个 RecyclerView。单击其任何项目后,我使用 AlertDialogShow#UpdateStudent() 方法更新该项目。我的问题是我无法使用 notifyItemChanged() 刷新 UpdateStudent() 方法中的 Adapter

如何从无法直接访问 Adapter 的另一个 class 刷新 Adapter

AdapterActivityRecyclerView:

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            AlertDialogShow show =
                new AlertDialogShow(context, database, studentData, performanceData);
            show.UpdateStudent(studentName, studentId, classId, position);
        }
    }
}

AlertDialogShow class:

public class AlertDialogShow {

    Context context;
    DatabaseHandler database;
    List<StudentTable> studentData;

    public AlertDialogShow(Context context, DatabaseHandler database,
                           List<StudentTable> studentData , List<PerformanceTable> performanceData) {
        this.context = context;
        this.database = database;
        this.studentData = studentData;
        this.performanceData = performanceData;
    }

    public AlertDialog UpdateStudent(final String studentName, final String studentId, 
                                     final int classId , final int position) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        View viewLayout = LayoutInflater.from(context)
                                        .inflate(R.layout.alertdialog_edit_class_or_student , null);
        dialog.setView(viewLayout);

        final AlertDialog alertDialog = dialog.create();

        Button editStudent_btn = (Button)viewLayout.findViewById(R.id.item_btn_EditClassStudent_Edit);

        editStudent_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    database.UpdateStudentDatabase(classId , studentId_new , studentId, studentName_new);

                    StudentTable studentTable = studentData.get(position);
                    studentTable.setStudentName(studentName_new);
                    studentTable.setStudentId(studentId_new);
                    studentData.set(position , studentTable);
                    //notifyItemChanged(position); <-- cannot define this line
                }
            }
        );

        return alertDialog;
    }
}

更新 RecyclerView 适配器只能在适配器本身或通过您的 Activity 中的适配器实例完成。要访问这些方法,您需要使用如下接口:

public class AlertDialogShow {

    public interface OnItemChange {
        void notifyAdapter(int position);
    }

    private OnItemChange listener;

    public AlertDialogShow(...) {
        this.listener = (MyActivity)context;
    }
}

然后在您的 Activity 中为 OnItemChange 接口编写一个实现,如下所示:

public class MyActivity extends ... implements AlertDialogShow.OnItemChange {

    @Override 
    public void notifyAdapter(int position) {
        // Notify your adapter item change here
        // e.g.: adapter.notifyItemChanged(position);
    }
}

然后您可以在 AlertDialogShow class 中使用 OnItemChange 侦听器,如下所示:

this.listener.notifyAdapter(position);

这将调用 Activity 中的 notifyAdapter(int position) 方法并执行您在那里编写的代码。


祝你好运。