android - 如何使用 DiffUtil 获取插入项的位置?

标签 android android-recyclerview

一段时间以来,我一直被困在一个相当简单的问题上。出于某种原因,我无法在 StackOverflow 上找到这方面的答案,所以我想我应该直接寻求帮助。

问题很简单,我有一个按日期排序的交易对象列表。我正在向列表中添加一个新的事务,然后求助于它。由于我不知道添加新交易的位置,我正在尝试使用 DiffUtil 为我找出它并适本地更新 RecyclerView 内容。

但是,我得到的结果真的很奇怪。出于某种原因,整个列表都在更新,顺序变得完全困惑。然而,基础数据是正确的。

更新列表之前: enter image description here

更新列表后: enter image description here 这是我的 Adapter 中用于添加新交易项目的代码。

void addItem(Transaction transaction) {
    ArrayList<Transaction> oldList = new ArrayList<>(transactions);
    transactions.add(transaction);
    Collections.sort(transactions);

    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallback(oldList, transactions), false);
    diffResult.dispatchUpdatesTo(this);
}

最后,这是我的 DiffUtilCallback 实现

class DiffCallback extends DiffUtil.Callback {

    private List<Transaction> oldList;
    private List<Transaction> newList;

    public DiffCallback(List<Transaction> oldList, List<Transaction> newList) {
        this.oldList = oldList;
        this.newList = newList;
    }

    @Override
    public int getOldListSize() {
        return oldList.size();
    }

    @Override
    public int getNewListSize() {
        return newList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        Transaction oldItem = oldList.get(oldItemPosition);
        Transaction newItem = newList.get(oldItemPosition);
        return oldItem.equals(newItem);
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        Transaction oldItem = oldList.get(oldItemPosition);
        Transaction newItem = newList.get(oldItemPosition);
        return oldItem.equals(newItem);
    }
}

这是 Transaction

public class Transaction implements Comparable<Transaction> {

public String category;
public String description;
public Date date;
public Double amount;

public Transaction(String description, Date date, Double amount) {
    this.category = "";
    this.description = description;
    this.date = date;
    this.amount = amount;
}

public Transaction(String category, String description, Date date, Double amount) {
    this.category = category;
    this.description = description;
    this.date = date;
    this.amount = amount;
}

@Override
public int compareTo(@NonNull Transaction transaction) {
    if (this.category.compareTo(transaction.category) == 0) {
        return this.date.compareTo(transaction.date);
    }
    return this.category.compareTo(transaction.category);
}

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Transaction)) {
        return false;
    }

    Transaction other = (Transaction)obj;
    return this.category.equals(other.category)
        && this.description.equals(other.description)
        && this.date.equals(other.date)
        && this.amount.equals(other.amount);
}

@Override
public String toString() {
    return description;
}
}

如有任何帮助,我们将不胜感激。如果需要更多信息来澄清问题,请告诉我。

最佳答案

areContentsTheSame 中,您应该比较 Transaction 的属性,但您正在比较 Transaction 对象本身。

更新areContentsTheSame

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
    Transaction oldItem = oldList.get(oldItemPosition);
    Transaction newItem = newList.get(oldItemPosition);

    return oldItem.category == newItem.category  && oldItem.description== newItem.description && oldItem.date== newItem.date && oldItem.amount == newItem.amount;
}

关于android - 如何使用 DiffUtil 获取插入项的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730655/

相关文章:

android - Navigation.createNavigateOnClickListener 来自 onBindViewHolder?

java - Gson - 从 Json 转换为类型化的 ArrayList<T>

android - 是否可以将 Android View 渲染到 OpenGL FBO 或纹理?

android - adt插件更新失败

java - 如何跟踪 Android 中 RecyclerView 中的选定项目?

android - 如何使用 Switch 基于字符串对 RecyclerView 中的 CardView 进行排序?

android - 连接到网络

android - 我如何在Android编程中获取TextView的默认值?

android - 如何使用 GridLayoutManager 设置每行不同的列数

android - 非法参数异常 : LinearLayoutManager is already attached to a RecyclerView