Realm Change Listener with RealmResults not being called

戏子无情 提交于 2019-11-29 07:46:52

You need to store the RealmResults as a field reference in order to ensure that Realm can update it

RealmResults<Record> realmResults;

public void etc() {
    realmResults = RealmManager.recordsDao().loadRecords();
    realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() {

Also, you should probably use RealmRecyclerViewAdapter from https://github.com/realm/realm-android-adapters with RealmResults<Record>, instead of ArrayList so that you actually keep a managed results in sync with your recycler view automatically


So with that in mind, all you need to do is replace your code with

public class RecordsAdapter extends RealmRecyclerViweAdapter<Record, RecordsAdapter.ViewHolder> {
     public RecordsAdapter(OrderedRealmCollection<Record> realmResults) {
         super(realmResults, true);
     }

     // ... same as before
}

and

recyclerView.setAdapter(new RecordsAdapter(RealmManager.recordsDao().loadRecords());

And just ditch your RealmChangeListener because it is incomplete and unnecessary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!