从另一个类调用RecyclerView.Adapter上的notifyItemChanged() [英] Calling notifyItemChanged() on a RecyclerView.Adapter from another class

查看:275
本文介绍了从另一个类调用RecyclerView.Adapter上的notifyItemChanged()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a RecyclerView in AdapterActivity. After clicking on any of its items, I update that item using my AlertDialogShow#UpdateStudent() method. My problem is that I can not refresh the Adapter inside the UpdateStudent() method using notifyItemChanged().

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

How can I refresh the Adapter from another class that does not have direct access to the Adapter?

AdapterActivity中与RecyclerView:

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类:

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

推荐答案

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

Updating RecyclerView adapter can only be done in adapter itself or through adapters instance in your Activity. To reach those methods you need to use interfaces like so:

public class AlertDialogShow {

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

    private OnItemChange listener;

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

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

Then write an implementation for OnItemChange interface in your Activity like so:

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类中使用OnItemChange侦听器,如下所示:

Then you can use OnItemChange listener in your AlertDialogShow class like so:

this.listener.notifyAdapter(position);

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

This will call notifyAdapter(int position) method in your Activity and execute code that you have written there.

祝你好运.

这篇关于从另一个类调用RecyclerView.Adapter上的notifyItemChanged()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆