Realm 怎么与 RecyclerView 配合使用

Realm 官方有介绍 RealmBaseAdapter with ListView 以及 RealmRecyclerViewAdapter with RecyclerView 的使用。

不过其实 Realm 是可以配合普通的 RecyclerView.Adapter 使用的,关键在于大家可能不太能理解把一个 RealmList<T> 或者 RealmResults<T> 传到 Adapter 里,怎么当成 List<T> 处理呢?其实也没有想象中那么复杂,但有四个需要注意的点,我在下面的示例代码中标注了:

// MainActivity.class
public class MainActivity {

    // ...
    // Assumed a javabean named School.class and has 2 fields:
    // (@PrimaryKey) long id and RealmList<Student> studentList.

    private void showRecyclerView() {
        Realm realm = Realm.getDefaultInstance();

        // Here we just take one School instance to show this demo
        School school = realm.where(School.class).findFirst();
        // RealmResults<School> schools = realm.where(School.class).findAll();

        // [Notice 1] here we got a RealmList<T>
        RealmList<Student> studentList = school.getStudentList();
        MyAdapter adapter = new MyAdapter(studentList);

        // ...
        // recycler.setAdapter(adapter)...

    }
}
// MyAdapter.class
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<Student> studentList = new ArrayList<>();
    // [Notice 2] don't forget to assign it as "new ArrayList<>()"

    // The constructor
    // [Notice 3] the constructor's argument is List<T>, not RealmList<T>
    // Because RealmList has already implemented List<T>, so this conversion is allowed 
    // (just like "View view = mTextView;", hope you can understand it)
    public MyAdapter(List<Student> studentList) {
        // [Notice 4]
        // Wrong:
        // this.studentList = studentList;
        // Right:
        this.studentList.addAll(studentList);

        // 为什么要用 addAll() 而不是 等于号 呢?
        // 因为后者相当于引用,前者才是复制一份新的 list
        // 在加载列表(onBindView)的时候大家可能看不出区别,因为无论是用 addAll() 还是 等于号 都能正常加载;
        // 但是当我们对列表进行增删操作时,后者就会报错了,因为 Realm 不允许我们直接对着 Pointer 进行操作
        // 比如下面写的 addItem(),就是一个会引起错误的地方。因此这里一定要用 addAll() 赋值才能避免问题
    }


    public void addItem(int position, Student student) {
        studentList.add(position, student);
        notifyItemInserted(position); // notify... 是自带的方法
    }

    /*public void removeItem(int position) {
        studentList.remove(position);
        notifyItemRemoved(position);
    }*/


    // Normally overriding operations...

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // Assumed Student.class has 2 members:
        // String studentName and String studentGender
        holder.studentName.setText(studentList.get(position).getStudentName());
        holder.studentGender.setText(studentList.get(position).getStudentGender());
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }


    // ViewHolder constructor
    static class ViewHolder extends RecyclerView.ViewHolder {
        ...
    }
}

参考

阅读更多
版权声明:本文为博主原创文章,转载请保留原作者名和原文链接。 https://blog.csdn.net/Likianta/article/details/80319989
个人分类: [08] Realm
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭