diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListAdapter.java b/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListAdapter.java index 1245668a0..e036f859c 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListAdapter.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListAdapter.java @@ -105,11 +105,10 @@ public class HabitCardListAdapter * Returns the item that occupies a certain position on the list * * @param position position of the item - * @return the item at given position - * @throws IndexOutOfBoundsException if position is not valid + * @return the item at given position or null if position is invalid */ @Deprecated - @NonNull + @Nullable public Habit getItem(int position) { return cache.getHabitByPosition(position); @@ -314,6 +313,8 @@ public class HabitCardListAdapter public void toggleSelection(int position) { Habit h = getItem(position); + if (h == null) return; + int k = selected.indexOf(h); if (k < 0) selected.add(h); else selected.remove(h); diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListCache.java b/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListCache.java index fbb4e51a6..228700768 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListCache.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/list/model/HabitCardListCache.java @@ -93,12 +93,12 @@ public class HabitCardListCache implements CommandRunner.Listener * Returns the habits that occupies a certain position on the list. * * @param position the position of the habit - * @return the habit at given position - * @throws IndexOutOfBoundsException if position is not valid + * @return the habit at given position or null if position is invalid */ - @NonNull - public Habit getHabitByPosition(int position) + @Nullable + public synchronized Habit getHabitByPosition(int position) { + if(position < 0 || position >= data.habits.size()) return null; return data.habits.get(position); }