我只是尝试按照创建时间戳在RecyclerView
中对项目进行排序。首先,它似乎工作正常,但有一个奇怪的错误有时会在从<{1}}删除项目时发生。
My RealmAdapter如下所示:
RecyclerView
将数据绑定到public class RealmMedicineListAdapter extends RealmRecyclerViewAdapter<Medicine, RealmMedicineListAdapter.ViewHolder> {
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item_bullet_list_medicine_tv_title)
TextView tvMedicineTitle;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
void bindData(@NonNull Medicine medicine) {
tvMedicineTitle.setText(medicine.getTitle());
}
}
public RealmMedicineListAdapter(@Nullable OrderedRealmCollection<Medicine> data, boolean autoUpdate) {
super(data, autoUpdate);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_bullet_list_medicine, parent, false);
return new ViewHolder(row);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Medicine medicine = getItem(position);
if(medicine == null)
throw new IllegalStateException("'medicine may not be null'");
holder.bindData(medicine);
}
}
:
adapter
每当创建新的RealmList<Medicine> consumedMedicine;
// ...
medicineListAdapter.updateData(consumedMedicine.sort("timeStamp"));
项时,都会生成时间戳:
Medicine
即使将新项目添加到public Medicine() {
timeStamp = new Date().getTime();
}
列表,排序也能正常工作,但是当从该列表中删除项目时,它会变得奇怪。有时,并非总是如此,当我从列表中删除项目时,错误的项目将被删除。
这张图片应该表达我的意思: https://ibb.co/hTmUoF
删除项目的代码如下:
consumedMedicine
注意: // Callback when the user deletes a row item in the RecyclerView
realm.executeTransaction(realm -> medicineEntry.deleteMedicine(realm, position));
// ...
public void deleteMedicine(int position) {
if(position < 0 || position >= consumedMedicine.size()) {
throw new IllegalArgumentException("'position' must be in range of [0, "
+ consumedMedicine.size() + "[ --- passed 'position': " + position);
}
Medicine itemToDelete = consumedMedicine.get(position);
if(!isUndoPointSet) {
//Not important for now!
LoggingUtils.databaseAction("DELETED medicine item with TITLE " + itemToDelete.getTitle() + " from DB.");
itemToDelete.deleteFromRealm();
} else {
//NOTE: This might be the code that causes this unwanted behaviour.
itemToDelete.setPendingDelete(true);
pendingList.add(itemToDelete);
consumedMedicine.remove(itemToDelete);
LoggingUtils.databaseAction("Medicine item TITLE " + itemToDelete.getTitle() + " is now PENDING_DELETE.");
}
是pendingList
,其中包含用户新添加或删除的所有项目(此项目标记为'pending'-flags ),以便以后可以撤消这些行动。例如。如果用户决定保存他的行为,则所有带有“pendingDelete”标志的项目将从数据库等中删除。
就我而言,RealmList<Medicine>
的不受欢迎的行为只有在RecyclerView
为真时才会发生。
这是因为isUndoPointSet
中的顺序与排序的RealmRecyclerViewAdapter
列表不一样吗?但是自动更新功能应该注意这一点,还是我错了?感谢。
编辑:我想我现在已经修好了,我已经试过几次了,现在看起来真的有用了。
consumedMedicine
我认为问题如下:适配器代表已排序 public void deleteMedicine(@NonNull Realm realm, int position) {
if(position < 0 || position >= consumedMedicine.size()) {
throw new IllegalArgumentException("'position' must be in range of [0, "
+ consumedMedicine.size() + "[ --- passed 'position': " + position);
}
//This caused the problems!
//Medicine itemToDelete = consumedMedicine.get(position);
//The below line is the important change!
Medicine itemToDelete = getMedicineSortedByTime().get(position);
if(!isUndoPointSet) {
LoggingUtils.databaseAction("DELETED medicine item at with TITLE " + itemToDelete.getTitle() + " at pos " + position + " from DB.");
itemToDelete.deleteFromRealm();
} else {
itemToDelete.setPendingDelete(true);
pendingList.add(itemToDelete);
consumedMedicine.remove(itemToDelete);
LoggingUtils.databaseAction("Medicine item TITLE " + itemToDelete.getTitle() + " on pos " + position + " is now PENDING_DELETE.");
}
}
列表。当我从列表中删除某个项目时,我已在已排序列表中指定了该项目的位置,但我从未排序列表中删除了consumedMedicine
处的项目。这似乎解决了这个问题。