Create a new list when building suggestion data.

- using sublist of the original suggestions list may results in
ConcurrentModificationException if the originaly list is being modified.
Create a new list instead to avoid running into the issue.

Change-Id: Ia83a0432be542eeb428d177f6118d26fc2262e93
Fixes: 74194336
Test: run monkey
This commit is contained in:
Doris Ling
2018-03-06 10:55:40 -08:00
parent 59520b028a
commit 05811609c7
3 changed files with 8 additions and 5 deletions

View File

@@ -155,8 +155,8 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
// remain as the dashboard item. Need to refresh the dashboard list. // remain as the dashboard item. Need to refresh the dashboard list.
setSuggestions(null); setSuggestions(null);
} else { } else {
mSuggestionAdapter.removeSuggestion(suggestion); list.remove(suggestion);
notifyItemChanged(0, null); setSuggestions(list);
} }
} }

View File

@@ -246,7 +246,11 @@ public class DashboardData {
if (suggestions.size() <= MAX_SUGGESTION_COUNT) { if (suggestions.size() <= MAX_SUGGESTION_COUNT) {
return suggestions; return suggestions;
} }
return suggestions.subList(0, MAX_SUGGESTION_COUNT); final List<Suggestion> suggestionsToShow = new ArrayList<>(MAX_SUGGESTION_COUNT);
for (int i = 0; i < MAX_SUGGESTION_COUNT; i++) {
suggestionsToShow.add(suggestions.get(i));
}
return suggestionsToShow;
} }
/** /**

View File

@@ -124,10 +124,9 @@ public class DashboardAdapterTest {
final Suggestion suggestionToRemove = suggestions.get(1); final Suggestion suggestionToRemove = suggestions.get(1);
adapter.onSuggestionClosed(suggestionToRemove); adapter.onSuggestionClosed(suggestionToRemove);
assertThat(adapter.mDashboardData).isEqualTo(dashboardData);
assertThat(suggestions.size()).isEqualTo(2); assertThat(suggestions.size()).isEqualTo(2);
assertThat(suggestions.contains(suggestionToRemove)).isFalse(); assertThat(suggestions.contains(suggestionToRemove)).isFalse();
verify(adapter, never()).notifyDashboardDataChanged(any()); verify(adapter).notifyDashboardDataChanged(any());
} }
@Test @Test