2016-06-29 5 views
0

私はsuggestedFriendsallSuggestedFriendsの2つのリストを持っています。Android:ConcurrentModificationException継続

イテレータを使用して最初のリストを実行し、このリストからアイテムを削除します。途中でアイテムがまだ残っている場合、アイテムは2番目のリストから削除する必要があります。 2番目のリストから項目を削除するために、2番目のイテレータ(SafeRemoveメソッド内)を使用します。

問題:項目を削除するためにイテレータを使用していますが、if (allSuggestedFriends.size() > 0) {という行にConcurrentModificationExceptionが表示されることがあります。

イテレータを使用してサイズを問い合わせる必要がありますか?更新

private void SafeRemove(List<User> list, User friend) { 
    Iterator<User> iter = list.iterator(); 

    while (iter.hasNext()) { 
     User user = iter.next(); 

     if (user.userId == friend.userId) 
      iter.remove(); 
    } 
} 

​​を追加する解決策になる私は、第二のイテレータを使用して(?!)

synchronized (suggestedFriends) { 
    for (final Iterator<User> suggestedFriendsIterator = suggestedFriends.iterator(); suggestedFriendsIterator.hasNext();) { 
     User friend = suggestedFriendsIterator.next(); 
     if (friend.userId == request.getUserId()) { 
      final int index = suggestedFriends.indexOf(friend); 
      if (status) { 
       ((FragmentActivityExt) context).runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         if(suggestedFriends.size() > 0) { 
          suggestedFriendsIterator.remove(); 
         } 
         notifyDataSetChanged(); 
         if (allSuggestedFriends.size() > 0) { 
          suggestedFriends.add(2, allSuggestedFriends.get(0)); 
          SafeRemove(allSuggestedFriends, allSuggestedFriends.get(0)); 
          notifyDataSetChanged(); 
         } 
         if (suggestedFriends.size() == 1) { 
          // FIXME workaround to fix list height as wrap_content is not supported by RecyclerView at the moment 
          //set to fit 1 element 
          LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mListView.getLayoutParams(); 
          params.height = EndoUtility.dpToPx(context, 70); 
          mListView.setLayoutParams(params); 
         } 

         if (suggestedFriends.size() == 0) { 
          EventBus.getDefault().post(new NoMoreSuggestedFriendsEvent()); 
         } 
        } 
       }); 
      } else { 
       if (mListView != null) { 
        ((FragmentActivityExt) context).runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(context, R.string.networkProblemToast, Toast.LENGTH_LONG).show(); 
          ((SuggestedFriendView) mListView.getChildAt(index)).reset(); 
         } 
        }); 
       } 
      } 
      break; 
     } 
    } 
} 

にそれが可能だSafeRemove方法を考えていませんSafeRemove(allSuggestedFriends, allSuggestedFriends.get(0));行の周りに?次のようになります。

... 
synchronized (allSuggestedFriends) { 
    SafeRemove(allSuggestedFriends, allSuggestedFriends.get(0)); 
} 
... 

以上、SafeRemoveメソッドの内部では?このように:

private void SafeRemove(List<User> list, User friend) { 
    synchronized (list) { 
     Iterator<User> iter = list.iterator(); 

     while (iter.hasNext()) { 
      User user = iter.next(); 

      if (user.userId == friend.userId) 
       iter.remove(); 
     }    
    } 
} 
+0

可能な複製(http://stackoverflow.com/questions/6866238/concurrent-modification-exception-adding-to-an-arraylist) –

+0

できるすべてのコメント私の更新? – Ambran

答えて

0

私の質問は潜在的な重複としてマークされているので、私はこれまでどんな反応も得ていませんでした。これは、議論のケースが提案されたリンクとは異なることを確信しているので、残念です。

しかし、私は​​の実装されたキーワードを使用しませんでしたが(私自身が私のアップデートで示唆したように)、リストがロード完了するとリストサイズに収まる並列カウンタを実装しました。リストから削除されました。これにより、ConcurrentModificationExceptionが発生したallSuggestedFriends.size()チェックが削除されます。

... 
private int allSuggestedFriendsCount; 
... 
// allSuggestedFriends loads items 
... 
allSuggestedFriendsCount = allSuggestedFriends.size(); 
... 

... 
if (allSuggestedFriendsCount > 0) { 
    suggestedFriends.add(2, allSuggestedFriends.get(0)); 
    SafeRemove(allSuggestedFriends, allSuggestedFriends.get(0)); 
    notifyDataSetChanged(); 
} 
... 

private void SafeRemove(List<User> list, User friend) { 
    Iterator<User> iter = list.iterator(); 

    while (iter.hasNext()) { 
     User user = iter.next(); 
     if (user.userId == friend.userId) { 
      iter.remove(); 
      allSuggestedFriendsCount--; 
     } 
    } 
} 
[同時変更例外:ArrayListに追加する]の