使用subList的ConcurrentModificationException?

时间:2017-06-09 17:29:53

标签: java android android-activity android-recyclerview

我有一个常规的RecyclerView。

我的RecyclerView有一个Activity监听的监听器界面。单击RecyclerView中的项目时,将调用“onItemClick()”事件。

调用此事件时,我需要从RecyclerView中删除接下来的5个项目,然后将它们添加到MapMap<Integer, List<Object>>)中。如果再次点击RecyclerView中的相同项目,我希望将5个项目重新添加到RecyclerView。

以下是我的代码在我的活动中的样子(请注意我删除了所有不相关的代码,例如onCreate()方法等):

public class MainActivity extends AppCompatActivity implements MyAdapter.MyAdapterListener {

    // The RecyclerView data
    private List<Object> data = new ArrayList<>();

    // The Map to keep track of all removed data
    private Map<Integer, List<Object>> removedData = new HashMap<>();

    private RecyclerView recyclerView;

    private LinearLayoutManager linearLayoutManager;

    private MyAdapter myAdapter;

    @Override
    public void onItemClick(int position) {
        Item item = (Item) data.get(position);

        // Check if the Map key/value pair already exists
        if (removedData.get(item.getId()) != null) {
            // Key/value pair exists, re-add the 5 items into the
            // RecyclerView and remove the key/value pair from the Map

            List<Object> fiveItems = removedData.get(item.getId());

            // Re-add the five items to the RecyclerView
            data.addAll(position, fiveItems);

            // Remove the key/value pair from the Map
            removedData.remove(item.getId());
        } else {
            // Key/value pair doesn't exist, remove the next 5 items
            // from the RecyclerView and add it to the Map

            int start = position + 1;
            int end = position + 5;

            List<Object> fiveItems = data.subList(start, end);

            // Add the five items to the Map
            removedData.put(item.getId(), fiveItems);

            // Remove the items from the RecyclerView
            data.subList(start, end).clear();
        }

        // Notify data set changed
        myAdapter.notifyDataSetChanged();
    }
}

但是,我在第data.addAll(position, fiveItems)行上收到了此错误:

06-FATAL EXCEPTION: main
java.util.ConcurrentModificationException
    at java.util.ArrayList$SubList.size(ArrayList.java:1028)
    at java.util.AbstractCollection.toArray(AbstractCollection.java:136)
    at java.util.ArrayList.addAll(ArrayList.java:589)
    at com.my.package.activity.MainActivity.onItemClicked(MainActivity.java:238)
    at com.my.package.adapter.MyAdapter$ViewHolderItem.onClick(MyAdapter.java:235)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

为什么我收到此错误?我该如何解决?

1 个答案:

答案 0 :(得分:1)

在你的data.addAll(position,fiveItems)方法内部使用Object [] a = c.toArray(),它使用Iterator it = iterator();.当你将使用迭代器时同时如果其他一个改变了相同的列表然后modCount将改变导致异常的。所以你需要找出负责它的代码。

toArray()来自java api的源代码

Object[] r = new Object[size()];
        Iterator<E> it = iterator();
        for (int i = 0; i < r.length; i++) {
            if (! it.hasNext()) // fewer elements than expected
                return Arrays.copyOf(r, i);
            r[i] = it.next();
        }
        return it.hasNext() ? finishToArray(r, it) : r;
相关问题