Reputation: 4719
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);
});
}
});
}
Upvotes: 4
Views: 3827
Reputation: 81539
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
Upvotes: 9