RealmResults未被调用的Realm Change Listener

时间:2017-03-13 10:46:00

标签: android realm

我正在使用Realm 3.0.0

我从Realm中获取一些对象并尝试添加onChangeListener,但是在更改对象时不会触发它

这里是代码,我在这里遗漏了什么吗?

 RealmResults<Record> realmResults = RealmManager.recordsDao().loadRecords();
    realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() {
        @Override
        public void onChange(RealmResults<Record> element) {
            for (int i = 0; i < recordList.size(); i++) {
                if (collection.get(i).getId().equals(recordList.get(i).getId())) {
                    recordList.set(i, collection.get(i));
                    adapter.notifyItemChanged(i);
                }

            }
        }
    });

同样根据文档,它提到了invalidateView(); ,但即使这样也不能反映新数据

对象的更改是在适配器

中进行的
public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.ViewHolder> {

private ArrayList<Record> recordList;
private Context context;
private Realm realm = Realm.getDefaultInstance();

public RecordsAdapter(ArrayList<Record> recordList, Context context) {
    this.recordList = recordList;
    this.context = context;
}

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


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.batsmanName.setText(recordList.get(position).getName());
    Glide.with(context).load(recordList.get(position).getImage()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.profilePicture);
    holder.totalRuns.setText("Runs " + recordList.get(position).getTotalScore());
    holder.totalMatches.setText("Matches " +recordList.get(position).getMatchesPlayed());


    if (recordList.get(position).isFavourite())
        holder.favCheck.setChecked(true);
    else
        holder.favCheck.setChecked(false);


    holder.favCheck.setOnClickListener(v -> {
        CheckBox checkBox = (CheckBox) v;
        if (checkBox.isChecked()) {
            realm.executeTransaction(realm1 -> {
                recordList.get(position).setFavourite(true);
                realm.copyToRealmOrUpdate(recordList);
            });
        } else {
            realm.executeTransaction(realm12 -> {
                recordList.get(position).setFavourite(false);
                realm.copyToRealmOrUpdate(recordList);
            });
        }

    });


}

1 个答案:

答案 0 :(得分:9)

您需要将RealmResults存储为字段引用,以确保Realm可以更新它

$(".Form-field input[type='email']").val();

此外,您应该使用https://github.com/realm/realm-android-adapters中的RealmResults<Record> realmResults; public void etc() { realmResults = RealmManager.recordsDao().loadRecords(); realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() { RealmRecyclerViewAdapter代替RealmResults<Record>,而不是ArrayList,这样您实际上可以自动将管理结果与您的回收站视图同步< / p>

因此,考虑到这一点,您需要做的就是用

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

     // ... same as before
}

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

然后放弃你的RealmChangeListener,因为它不完整且没必要