1

So I'm having an issue and I can't seem to find a cause for it so I'm here asking for some help. I was able to find where the problem is coming from, but I can't find why it happens, here's what I have.

This is a sample of what I'm trying to save in the sharedpreferences.

public class Registo implements Serializable {
    int id;
    String name;
    ArrayList<Integer> commitedViolations= new ArrayList<>();
}

I do all the process of saving it

public void addRegisto(Context context, Registo registo) {
        ArrayList<Registo> registos = getRegistos(context);
        if (registos == null)
            registos = new ArrayList<>();
        registos.add(registo);
        saveRegistos(context, registos);
    }



public void saveRegistos(Context context, ArrayList<Registo> favorites) {
    // used for store arrayList in json format
    SharedPreferences settings;
    SharedPreferences.Editor editor;

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();

    Gson gson = new Gson();
    String jsonFavorites = gson.toJson(favorites);

    editor.putString(REGISTOS, jsonFavorites);

    editor.apply();
}

And here's where magic happens, when I retrieve the objects I saved in the device, the arrayList of the object that was saved with the following values [200, 201, 202], now returns them as Double values [200.0, 201.0, 202.0].

I made a workaround, but I wanna know why this happens.

public ArrayList<Registo> getRegistos(Context context) {
    SharedPreferences settings;
    List<Registo> registos;

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    if (settings.contains(REGISTOS)) {
        String jsonFavorites = settings.getString(REGISTOS, null);
        Gson gson = new Gson();
        Registo[] registosItems = gson.fromJson(jsonFavorites, Registo[].class);

        // <WORKAROUND>
            for (Registo registosItem : registosItems) {
                for (int i = 0; i < registosItem.getCommitedViolations().size(); i++) {
                    String str = String.valueOf(registosItem.getCommitedViolations().get(i));
                    double valueAsDouble = Double.parseDouble(str);
                    int valueAsInt= (int) valueAsDouble;
                    registosItem.getCommitedViolations().set(i, valueAsInt);
                }
            }
        // </WORKAROUND>


        registos = Arrays.asList(registosItems);
        registos = new ArrayList<>(registos);
    } else
        return null;

    return (ArrayList<Registo>) registos;
}

PS. And the weirdest part about this is that the debug version works perfectly, this bug only happens on the release version.

2 Answers2

2

This is a problem with gson and has nothing to do with SharedPreferences, since they will not modify the String you are saving.

The error lies within gson serializing and deserializing Integer. You can see one question about this here.

Community
  • 1
  • 1
David Medenjak
  • 33,993
  • 14
  • 106
  • 134
0
        for (Registo registosItem : registosItems) {
            for (int i = 0; i < registosItem.getCommitedViolations().size(); i++) {
                String str = String.valueOf(registosItem.getCommitedViolations().get(i));
                int valueAsInt= Integer.parseInt(str);
                registosItem.getCommitedViolations().set(i, valueAsInt);
            }
        }

This should fix your issue regarding the doubles, why don't you parse the string directly?

gi097
  • 7,313
  • 3
  • 27
  • 49