I'm using Realm 3.0.0
I'm fetching some objects from Realm and trying to add onChangeListener, but it does not get fired when an object is changed
Here's the code, am I missing something here?
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);
}
}
}
});
Also as per the docs, it mentions to call invalidateView();
but even that does not reflect the new data
The change to the object is made in the adapter
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);
});
}
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…