gpt4 book ai didi

java - 如何修复使用 DiffUtil 向 ListUpdateCallback 发送更新时得到的错误位置

转载 作者:行者123 更新时间:2023-11-30 12:05:02 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,用户可以在其中使用 EditText 同时修改多个 String 项目,所以我需要弄清楚发生了什么变化以便通知服务器了解更改(创建新项目、更新现有项目或删除后者),这就是为什么我使用 DiffUtilListUpdateCallback 来执行此操作。如果旧列表的大小为 2 项的问题;然后我删除了索引 0 处的项目,然后将 3 个项目添加到列表的末尾,我得到了 onInserted 的回调,其中的位置参数不正确,导致 IndexOutOfBoundsException,< em>(并且是删除旧列表中除最后一项之外的任何项目的行为)请看一下GIF that shows the problem .

我尝试了以下代码并对新列表进行了其他更改,例如在索引 1 处删除,然后将 3 个项目添加到列表末尾,它工作正常!

我正在使用的数组是 Answer 类型的,它是一个类:

public class Answer {
private String id;
private String questionId;
private String text;
private Integer count;
}

请注意,如果两个项目相同,则可以通过 id 识别 2 个不同的对象;然后可以通过文本识别内容。

DiffUtil.Callback

public class AnswersDiffCallback extends DiffUtil.Callback {

List<Answer> newAnswers;
List<Answer> oldAnswers;

public AnswersDiffCallback(List<Answer> newAnswers, List<Answer> oldAnswers) {
this.newAnswers = newAnswers;
this.oldAnswers = oldAnswers;
}

@Override
public int getOldListSize() {
return oldAnswers == null ? 0 : oldAnswers.size();
}

@Override
public int getNewListSize() {
return newAnswers == null ? 0 : newAnswers.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
Answer oldAnswer = oldAnswers.get(oldItemPosition);
Answer newAnswer = newAnswers.get(newItemPosition);
return Objects.equals(oldAnswer.getId(), newAnswer.getId());
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
Answer oldAnswer = oldAnswers.get(oldItemPosition);
Answer newAnswer = newAnswers.get(newItemPosition);
return Objects.equals(oldAnswer.getText(), newAnswer.getText());
}
}

在 ListUpdateCallback 中,我试图记录我收到的回调,以便在我与服务器通信之前测试它是否正常工作。

Log.d(TAG, "answers: oldAnswers = " + Utils.serializeObject(oldAnswers));
Log.d(TAG, "answers: newAnswers = " + Utils.serializeObject(newAnswers));
Log.d(TAG, "-----------------------------------------------------------------------------");
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AnswersDiffCallback(newAnswers, oldAnswers), true);
diffResult.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
try {
Log.d(TAG, String.format("onInserted: (position, count) = (%d, %d)", position, count));
for (int i = position; i < position + count; i++) {
Log.d(TAG, "onInserted: newAnswer.text = " + newAnswers.get(i).getText());
}
} catch (Exception ex) {
Log.e(TAG, "onInserted: Exception", ex);
}
Log.d(TAG, "-----------------------------------------------------------------------------");
}

@Override
public void onRemoved(int position, int count) {
try {
Log.d(TAG, "onRemoved: (position, count) = (" + position + ", " + count + ")");
for (int i = position; i < position + count; i++) {
Log.d(TAG, String.format("onRemoved: (oldAnswer.id, oldAnswer.text) = (%s, %s)", oldAnswers.get(i).getId(), oldAnswers.get(i).getText()));
}
} catch (Exception ex) {
Log.e(TAG, "onRemoved: Exception", ex);
}
Log.d(TAG, "-----------------------------------------------------------------------------");
}

@Override
public void onMoved(int fromPosition, int toPosition) {
try {
Log.d(TAG, "onMoved: (fromPosition, toPosition) = (" + fromPosition + ", " + toPosition + ")");
Log.d(TAG, String.format("onMoved: (oldAnswer.id, oldAnswer.text) = (%s, %s)", oldAnswers.get(fromPosition).getId(), oldAnswers.get(fromPosition).getText()));
Log.d(TAG, String.format("onMoved: (newAnswer.id, newAnswer.text) = (%s, %s)", newAnswers.get(toPosition).getId(), newAnswers.get(toPosition).getText()));
} catch (Exception ex) {
Log.e(TAG, "onMoved: Exception", ex);
}
Log.d(TAG, "-----------------------------------------------------------------------------");
}

@Override
public void onChanged(int position, int count, @Nullable Object payload) {
try {
Log.d(TAG, "onChanged: (position, count) = (" + position + ", " + count + ")");
for (int i = position; i < position + count; i++) {
Log.d(TAG, String.format("onChanged: (oldAnswer.id, oldAnswer.text) = (%s, %s)", oldAnswers.get(i).getId(), oldAnswers.get(i).getText()));
Log.d(TAG, String.format("onChanged: (newAnswer.id, newAnswer.text) = (%s, %s)", newAnswers.get(i).getId(), newAnswers.get(i).getText()));
}
} catch (Exception ex) {
Log.e(TAG, "onChanged: Exception", ex);
}
Log.d(TAG, "-----------------------------------------------------------------------------");
}
});

这里是 logcat,但我有异常(exception):

2019-06-19 16:32:00.461 24515-24515/com.example.myApp D/AdminQuestionsFragment: answers: oldAnswers = [{"count":0,"id":"5d09a1236969e249cca42e96","questionId":"5d09a1236969e249cca42e95","text":"old answer 0"},{"count":0,"id":"5d09a1236969e249cca42e97","questionId":"5d09a1236969e249cca42e95","text":"old answer 1"}]
2019-06-19 16:32:00.501 24515-24515/com.example.myApp D/AdminQuestionsFragment: answers: newAnswers = [{"count":0,"id":"5d09a1236969e249cca42e97","questionId":"5d09a1236969e249cca42e95","text":"old answer 1"},{"text":"new answer 0"},{"text":"new answer 1"},{"text":"new answer 2"}]
2019-06-19 16:32:00.501 24515-24515/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------
2019-06-19 16:35:19.550 24515-24515/com.example.myApp D/AdminQuestionsFragment: onInserted: (position, count) = (2, 3)
2019-06-19 16:35:19.550 24515-24515/com.example.myApp D/AdminQuestionsFragment: onInserted: newAnswer.text = new answer 1
2019-06-19 16:35:19.550 24515-24515/com.example.myApp D/AdminQuestionsFragment: onInserted: newAnswer.text = new answer 2
2019-06-19 16:35:19.599 24515-24515/com.example.myApp E/AdminQuestionsFragment: onInserted: Exception
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.myApp.views.AdminQuestionsFragment$5.onInserted(AdminQuestionsFragment.java:333)
at androidx.recyclerview.widget.BatchingListUpdateCallback.dispatchLastEvent(BatchingListUpdateCallback.java:61)
at androidx.recyclerview.widget.BatchingListUpdateCallback.onRemoved(BatchingListUpdateCallback.java:96)
at androidx.recyclerview.widget.DiffUtil$DiffResult.dispatchRemovals(DiffUtil.java:921)
at androidx.recyclerview.widget.DiffUtil$DiffResult.dispatchUpdatesTo(DiffUtil.java:836)
at com.example.myApp.views.AdminQuestionsFragment.lambda$onActivityResult$8$AdminQuestionsFragment(AdminQuestionsFragment.java:320)
at com.example.myApp.views.-$$Lambda$AdminQuestionsFragment$KZmQo8gdnjCYX1JsaACEVkjSd1s.onChanged(Unknown Source:8)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:424)
at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:376)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:188)
at androidx.lifecycle.LiveData.observe(LiveData.java:185)
at com.example.myApp.views.AdminQuestionsFragment.onActivityResult(AdminQuestionsFragment.java:387)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at android.app.Activity.dispatchActivityResult(Activity.java:7454)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-06-19 16:35:19.599 24515-24515/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------
2019-06-19 16:35:19.600 24515-24515/com.example.myApp D/AdminQuestionsFragment: onRemoved: (position, count) = (0, 1)
2019-06-19 16:35:19.603 24515-24515/com.example.myApp D/AdminQuestionsFragment: onRemoved: (oldAnswer.id, oldAnswer.text) = (5d09a1236969e249cca42e96, old answer 0)
2019-06-19 16:35:19.603 24515-24515/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------

问题出在这一行:

2019-06-19 16:35:19.550 24515-24515/com.example.myApp D/AdminQuestionsFragment: onInserted: (position, count) = (2, 3)

为什么位置不是1?

更新 1这是 logcat,如果我有 2 个项目的旧列表,我删除了索引 1 处的项目,然后将 3 个项目添加到列表的末尾:

2019-06-19 18:25:24.368 28118-28118/com.example.myApp D/AdminQuestionsFragment: answers: oldAnswers = [{"count":0,"id":"5d09a1236969e249cca42e96","questionId":"5d09a1236969e249cca42e95","text":"old answer 0"},{"count":0,"id":"5d09a1236969e249cca42e97","questionId":"5d09a1236969e249cca42e95","text":"old answer 1"}]
2019-06-19 18:25:24.370 28118-28118/com.example.myApp D/AdminQuestionsFragment: answers: newAnswers = [{"count":0,"id":"5d09a1236969e249cca42e96","questionId":"5d09a1236969e249cca42e95","text":"old answer 0"},{"text":"new answer 0"},{"text":"new answer 1"},{"text":"new answer 2"}]
2019-06-19 18:25:24.370 28118-28118/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------
2019-06-19 18:25:24.370 28118-28118/com.example.myApp D/AdminQuestionsFragment: onRemoved: (position, count) = (1, 1)
2019-06-19 18:25:24.371 28118-28118/com.example.myApp D/AdminQuestionsFragment: onRemoved: (oldAnswer.id, oldAnswer.text) = (5d09a1236969e249cca42e97, old answer 1)
2019-06-19 18:25:24.371 28118-28118/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------
2019-06-19 18:25:24.372 28118-28118/com.example.myApp D/AdminQuestionsFragment: onInserted: (position, count) = (1, 3)
2019-06-19 18:25:24.372 28118-28118/com.example.myApp D/AdminQuestionsFragment: onInserted: newAnswer.text = new answer 0
2019-06-19 18:25:24.372 28118-28118/com.example.myApp D/AdminQuestionsFragment: onInserted: newAnswer.text = new answer 1
2019-06-19 18:25:24.372 28118-28118/com.example.myApp D/AdminQuestionsFragment: onInserted: newAnswer.text = new answer 2
2019-06-19 18:25:24.372 28118-28118/com.example.myApp D/AdminQuestionsFragment: -----------------------------------------------------------------------------

最佳答案

这似乎是 ListUpdateCallback 的一个已知问题。在 onInserted 中,插入的 oldList 项目不可能识别其在 newList 中的相关位置。

https://issuetracker.google.com/issues/115701827

我的解决方案是:在 oldList 中插入一个 null-dummy 列表项,在完成 diffResult.dispatchUpdatesTo 后,在相同位置用 newList 项替换 null-dummy。

例如,我留下了一些调试代码和日志来确定问题:

    public static final class ListUpdate<T extends BaseIdentifier> implements ListUpdateCallback {

private final List<T> oldList;
private final List<T> newList;

public ListUpdate(@NonNull List<T> oldList, @NonNull List<T> newList) {
//logger.trace("ListUpdate" + System.lineSeparator() + "old={}" + System.lineSeparator() + "new={}", BaseIdentifier.toString(oldList), BaseIdentifier.toString(newList));

this.oldList = oldList;
this.newList = newList;
}

public int inserts = 0;

public boolean hasInserts() {
return inserts > 0;
}

public void finishInserts() {
if (inserts <= 0) {
return;
}

//logger.trace("finishInserts inserts={}", inserts);

ListIterator<T> oldListIterator = oldList.listIterator();
ListIterator<T> newListIterator = newList.listIterator();

while (inserts > 0 && oldListIterator.hasNext() && newListIterator.hasNext()) {
T oldItem = oldListIterator.next();
T newItem = newListIterator.next();

if (oldItem == null) {
//Replaces the last element returned by next()
oldListIterator.set((T) newItem.copy());
inserts--;
}
}

if (inserts > 0 || oldList.contains(null)) {
//There must be something wrong
logger.error("finishInserts inserts={} remaining", inserts);
}
}

/** {@inheritDoc} */
@Override
public void onInserted(int position, int count) {
//logger.trace("onInserted position={} count={}", position, count);

for (int i = 0; i < count; i++) {
/*
T item = newList.get(position + i);
oldList.add(position + i, (T) item.copy());
*/
//We don't know the related position of the newList, so we add null
oldList.add(position + i, null);
inserts++;
}
}

/** {@inheritDoc} */
@Override
public void onRemoved(int position, int count) {
//logger.trace("onRemoved position={} count={}", position, count);

for (int i = 0; i < count; i++) {
oldList.remove(position);
}
}

/** {@inheritDoc} */
@Override
public void onMoved(int fromPosition, int toPosition) {
//logger.trace("onMoved fromPosition={} toPosition={}", fromPosition, toPosition);

T item = oldList.remove(fromPosition);
oldList.add(toPosition, item);
}

/** {@inheritDoc} */
@Override
public void onChanged(int position, int count, Object payload) {
logger.trace("onChanged position={} count={}", position, count);

for (int i = 0; i < count; i++) {
T item = newList.get(position + i);
//noinspection unchecked
oldList.set(position + i, (T) item.copy());
}
}
}

像这样打电话

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new BaseIdentifier.BaseIdentifierDiffUtilCallback(oldProcessEvents, processEvents));
BaseIdentifier.ListUpdate<ProcessEvent> updater = new BaseIdentifier.ListUpdate<>(oldProcessEvents, processEvents);
diffResult.dispatchUpdatesTo(updater);
updater.finishInserts();

关于java - 如何修复使用 DiffUtil 向 ListUpdateCallback 发送更新时得到的错误位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com