Veeresh Charantimath
Veeresh Charantimath

Reputation: 4719

Realm Adapter 2.0

This may be considered a follow-up question from Realm Change Listener with RealmResults not being called

Now that I'm using the Realm Adapters (https://github.com/realm/realm-android-adapters), the Real-Time changes to the object are reflected correct.

Now I'm sorting a field by doing

   private void sortByRuns() {
    searchRecordList.clear();
    recordRealmResults = RealmManager.recordsDao().loadMostRuns();
    adapter.updateData(recordRealmResults);
}

Now the updateData method in-turn calls adapter.notifiedDataSetChanged(); But my adapter still contains the old data

Here's my adapter

public class RecordsAdapter extends RealmRecyclerViewAdapter<Record, RecordsAdapter.ViewHolder> {

private Context context;
private OrderedRealmCollection<Record> orderedRealmCollection;

public RecordsAdapter(OrderedRealmCollection<Record> orderedRealmCollection, Context context) {
    super(orderedRealmCollection, true);
    this.orderedRealmCollection = orderedRealmCollection;
    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(orderedRealmCollection.get(position).getName());
    Glide.with(context).load(orderedRealmCollection.get(position).getImage()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.profilePicture);
    holder.totalRuns.setText("Runs " + orderedRealmCollection.get(position).getTotalScore());
    holder.totalMatches.setText("Matches " + orderedRealmCollection.get(position).getMatchesPlayed());


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


    holder.favCheck.setOnClickListener(v -> {
        CheckBox checkBox = (CheckBox) v;
        if (checkBox.isChecked()) {
            RealmManager.recordsDao().saveToFavorites(orderedRealmCollection.get(position));
        } else {
            RealmManager.recordsDao().removeFromFavorites(orderedRealmCollection.get(position));
        }

    });
}


public class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.profile_picture)
    ImageView profilePicture;
    @BindView(R.id.batsman_name)
    TextView batsmanName;
    @BindView(R.id.check_fav)
    CheckBox favCheck;
    @BindView(R.id.runs)
    TextView totalRuns;
    @BindView(R.id.matches)
    TextView totalMatches;
    @BindView(R.id.card_view)
    CardView cardView;


    @OnClick(R.id.card_view)
    public void navigate() {
        EventBus.getDefault().post(new MyEvents.RecordEvent(orderedRealmCollection.get(getAdapterPosition()), profilePicture));
    }


    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

How do I reflect changes in the adapter when I want to push in a updated List?

Upvotes: 0

Views: 269

Answers (2)

Bao Chen
Bao Chen

Reputation: 1

Even you can just use getItem(position : Int) to getData what you want bind to ViewHolder!!

Just like

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Record record = getItem(position)
holder.batsmanName.setText(record.getName());
Glide.with(context).load(record.getImage())
.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.profilePicture);

holder.totalRuns.setText("Runs " + record.getTotalScore());
holder.totalMatches.setText("Matches " + record.getMatchesPlayed());
if (record.isFavourite())
   holder.favCheck.setChecked(true);
else
   holder.favCheck.setChecked(false);
   holder.favCheck.setOnClickListener(v -> {
        CheckBox checkBox = (CheckBox) v;
        if (checkBox.isChecked()) {
            RealmManager.recordsDao().saveToFavorites(record);
        } else {
            RealmManager.recordsDao().removeFromFavorites(record);
        }

    });
}

Upvotes: 0

EpicPandaForce
EpicPandaForce

Reputation: 81588

You never actually update variable orderedRealmCollection after setting this.orderedRealmCollection = orderedRealmCollection;

So just use getData() instead

//private OrderedRealmCollection<Record> orderedRealmCollection;

public RecordsAdapter(OrderedRealmCollection<Record> orderedRealmCollection, Context context) {
    super(orderedRealmCollection, true);
    //this.orderedRealmCollection = orderedRealmCollection;
    this.context = context;
}

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


        if (record.isFavourite())
            holder.favCheck.setChecked(true);
        else
            holder.favCheck.setChecked(false);


        holder.favCheck.setOnClickListener(v -> {
            CheckBox checkBox = (CheckBox) v;
            if (checkBox.isChecked()) {
                RealmManager.recordsDao().saveToFavorites(record);
            } else {
                RealmManager.recordsDao().removeFromFavorites(record);
            }

        });
    }

Upvotes: 2

Related Questions