Drag'n'Drop ConcurentModificationException [英] Drag'n'Drop ConcurentModificationException

查看:168
本文介绍了Drag'n'Drop ConcurentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OnDragListener:

OnDragListener:

@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_ENTERED:
            switch (v.getId()) {
                case R.id.delete_zone: {
                    addToShowCaseZone.setImageDrawable(getResources().getDrawable(R.drawable.showcase_2));
                    inAddToShowcasesZone = true;
                    break;
                }
                case MagazineGridAdapter.ID: {
                    enteredView = v;
                    break;
                }
            }
            return false;

        case DragEvent.ACTION_DRAG_EXITED: {
            switch (v.getId()) {
                case R.id.delete_zone: {
                    addToShowCaseZone.setImageDrawable(getResources().getDrawable(R.drawable.showcase_1));

                    inAddToShowcasesZone = false;
                    break;
                }
                case MagazineGridAdapter.ID: {
                    enteredView = null;
                    break;
                }
            }
            return true;
        }
        case DragEvent.ACTION_DRAG_STARTED:
            return true;

        case DragEvent.ACTION_DRAG_LOCATION:
            return false;

        case DragEvent.ACTION_DROP: {
            if (inAddToShowcasesZone) {
                final int position = gridView.getPositionForView(dragView);

                Magazine magazine = magazineAdapter.getItem(position);

                try {
                    new Magazine(magazine.getUrl().toString(), magazine.getImage(), magazine.getBackgroundNum(), magazine.getName());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                addToShowCaseZone.setImageDrawable(getResources().getDrawable(R.drawable.showcase_1));

                inAddToShowcasesZone = false;

                magazineAdapter.deleteFromList(position);

                return false;

            } else if(enteredView != null && !enteredView.equals(dragView)){
                ResourcesForNativeMagazines.swapItems(gridView.getPositionForView(dragView), gridView.getPositionForView(enteredView), tabNumber - 1);

                magazineAdapter.refreshValues(ResourcesForNativeMagazines.getMagazines(tabNumber - 1));

                enteredView = null;

                return false;
            }

            dragView.setVisibility(VISIBLE);

            return false;
        }
        default:
            dragView.setVisibility(VISIBLE);

            return true;

    }
}

适配器部分:

public void refreshValues(List<Magazine> magazines){
    this.magazines = new ArrayList<>(magazines);
    notifyDataSetChanged();
}

public void deleteFromList(int position){
    magazines.remove(position);
    notifyDataSetChanged();
}

有时这个代码在方法refreshValuews和deleteFromList中调用错误,当我删除项目时,这是stacktrace:

Sometimes this code calls error in methods refreshValuews and deleteFromList when i dropped the item, this is stacktrace for it:

java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806)
        at java.util.HashMap$KeyIterator.next(HashMap.java:833)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1172)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174)
        at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:4911)
        at android.view.ViewRootImpl.access$700(ViewRootImpl.java:94)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3188)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

我应该如何解决?有没有其他方法通过拖放来修改gridView中的项目?

How i should fix it? Is there any other way to modify items inside gridView by drag'n'drop?

推荐答案

我找到了一个解决方案,不会导致一个例外,你应该做下一步:

I found a solution, not to cause an exception you should do next:

public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_ENDED:{
            v.post(new Runnable{
                public void run() {
                    //SomeCode;
                }
            });
        break;
        }  
    }
}

这篇关于Drag'n'Drop ConcurentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆