这可能被视为来自Realm Change Listener with RealmResults not being called
的后续问题现在我正在使用领域适配器(https://github.com/realm/realm-android-adapters),对象的实时更改反映正确。
现在我正在通过
对字段进行排序 private void sortByRuns() {
searchRecordList.clear();
recordRealmResults = RealmManager.recordsDao().loadMostRuns();
adapter.updateData(recordRealmResults);
}
现在updateData方法又调用adapter.notifiedDataSetChanged(); 但我的适配器仍然包含旧数据
这是我的适配器
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);
}
}
当我想要推送更新的列表时,如何反映适配器中的更改?
答案 0 :(得分:2)
设置orderedRealmCollection
this.orderedRealmCollection = orderedRealmCollection;
所以只需使用getData()
代替
//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);
}
});
}
答案 1 :(得分:0)
即使您仅可以使用getItem(position:Int)来获取想要绑定到ViewHolder的数据!
就像
@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);
}
});
}