2014-09-01 60 views
4

OnDragListener:中拖放ConcurentModificationException

@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調用錯誤,當我放棄了這個項目,這是它的堆棧跟蹤:

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) 

我該如何解決它?是否有任何其他方式通過drag'n'drop修改gridView內的項目?

+0

我有同樣的問題。當DRAG_ENDED上的dragView.setVisibility(VISIBLE)時它崩潰。任何想法? – 2014-10-16 12:04:08

+0

[這裏是固定的問題和其說明。] [1]:http://stackoverflow.com/questions/10988671/java-util-concurrentmodificationexception-in-view-setvisibility – 2014-10-16 12:11:52

回答

7

我發現了一個解決方案,而不是引發異常你應該下一步:

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

Concurrent修改異常在迭代列表時同時嘗試從列表中刪除項目時發生。

這可以使用迭代器來解決。

這是你可以使用一個迭代器:

Iterator<String> it = myArrayList.iterator(); 

while (it.hasNext()) { 
    String str = it.next(); 

    if (myCondition) 
     it.remove(); 
} 

參考流動環節

How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it?

Java : ConcurrentModificationException while iterating over list

+0

仍然沒有工作。 – Ufkoku 2014-09-01 10:22:18

+0

仍然給出相同的錯誤? – 2014-09-01 10:23:02

+0

是的,當我調用notifyDataSetChanged()時。 – Ufkoku 2014-09-01 10:27:28