もちろん、私はこのエラーの意味を知っていますが、これを削除する方法はわかりません。 今すぐ試用していますカスタムArrayListを持つjava.util.ConcurrentModificationException
private void removeFriendFromList() {
List<Friend> copy = new ArrayList<Friend>(globalSearchFriends);
for (Friend friend : globalSearchFriends) {
if (friend.equals(remove)) {
copy.remove(friend);
}
}
}
これは動作しません。 これは私のGLOBALLIST
private List<Friend> globalSearchFriends = new ArrayList<>();
である私はあまりにも反復処理しようとしているが、それはうまくいきませんでしたか、私は悪い何かをしました。
また、私はここでそれを使用する必要があります:私は、私はそのユーザーが表示されますが、EditTextでテキストを入力するときのように動作します。私は "アンドリュー"のような検索し、 "youko"私はエラーを取得します。
private void serachFriend(final String query) {
etGlobalSearch.addTextChangedListener(new TextWatcherAdapter() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
FindFriend request = new FindFriend();
request.query = query;
request.query = s.toString().toLowerCase().trim();
backend.findFriend(request).enqueue(new Callback<ResponseFindFriend>() {
@Override
public void onResponse(Call<ResponseFindFriend> call, Response<ResponseFindFriend> response) {
synchronized (globalSearchFriends) {
globalSearchFriends.clear();
removeFriendFromList();
try {
if (response == null)
throw new Exception();
if (!response.isSuccessful())
throw new Exception();
if (response.body() == null)
throw new Exception();
if (response.body().results == null)
throw new Exception();
globalSearchFriends = response.body().results;
} catch (Exception e) {
Log.d("Blad", "sobie");
} finally {
gatherResults();
}
}
}
@Override
public void onFailure(Call<ResponseFindFriend> call, Throwable t) {
synchronized (globalSearchFriends) {
globalSearchFriends.clear();
removeFriendFromList();
gatherResults();
}
}
});
}
});
}
private void removeFriendFromList() {
List<Friend> copy = new ArrayList<Friend>(globalSearchFriends);
for (Friend friend : globalSearchFriends) {
if (friend.equals(remove)) {
copy.remove(friend);
}
}
}
private void gatherResults() {
removeFriendFromList();
for (Friend f : globalSearchFriends)
globalSearchFriends.add(f);
findedFriendsAdapter.setFriendList(globalSearchFriends);
}
あらゆる種類のヘルプが関連付けられています。良い一日を! :)
編集 このケースでエラーが発生しました。
java.util.ConcurrentModificationException
for (Friend f : globalSearchFriends)
globalSearchFriends.add(f);
findedFriendsAdapter.setFriendList(globalSearchFriends);
と私が持っているログに:
at java.util.ArrayList$ArrayListIterator.next
リストを反復しながら変更するときは、イテレータを使用する必要があります。 – jitinsharma
** real ** [mcve]に一致するスタックトレースを提供してください。あるリストを反復するとき、別のリストから要素を削除するだけでうまくいくはずです。だから私は、あなたのコードは真実の部分だけを示していると仮定します。そして、記録のために:あなたはFriendsクラスの** override ** equalsをしましたか? – GhostCat
@jitinsharma彼は**反復中に**リストを変更していない**です。彼はそのリストの**コピー**を操作しています! – GhostCat