beekama / NutritionApp

Android-App for recording nutritional values in food and suggesting meals to compensate for deficiencies.
GNU General Public License v3.0
5 stars 0 forks source link

Adding Weight in Settings fails #17

Closed FAUSheppy closed 2 years ago

FAUSheppy commented 2 years ago

Adding a new weight entry fails if you:

This is because the recyler view reuses the existing ViewHolder and calls bindView instead of createView, which means the entryKey sets won't be updated with the new entry, meaning the List is empty and this happens:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.example.nutritionapp.WeightTrackingWeightListAdapter.onBindViewHolder(WeightTrackingWeightListAdapter.java:69)
FAUSheppy commented 2 years ago

And long story short: This is why one shouldn't create sub-variables for data-set entries, because if you then do this:

https://github.com/beekama/NutritionApp/blob/c29a8d313c17c3160c0d7c067c3bb2fabf7e6b6b/app/src/main/java/com/example/nutritionapp/WeightTracking.java#L199

It won't work :)

FAUSheppy commented 2 years ago

It's also unessesary to use a List for sorting at all, you can and should use a sorted Set like TreeSet:

TreeSet<String> set = new TreeSet<String>();
set.add("B");
set.add("A");
set.add("C");

for (String s : set) {
    System.out.println(s);
}

Output:

A
B
C
FAUSheppy commented 2 years ago

Fixed in: https://github.com/beekama/NutritionApp/commit/86d98a1873103cb2ca5b05cc0d24127285d0d578