java - Sharedpreferences saving arraylist<integer> with double values in release version -


so i'm having issue , can't seem find cause i'm here asking help. able find problem coming from, can't find why happens, here's have.

this sample of i'm trying save in sharedpreferences.

public class registo implements serializable {     int id;     string name;     arraylist<integer> commitedviolations= new arraylist<>(); } 

i 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 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 magic happens, when retrieve objects saved in device, arraylist of object saved following values [200, 201, 202], returns them double values [200.0, 201.0, 202.0].

i made workaround, wanna know why 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>             (registo registositem : registositems) {                 (int = 0; < 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. , weirdest part debug version works perfectly, bug happens on release version.

this problem gson , has nothing sharedpreferences, since not modify string saving.

the error lies within gson serializing , deserializing integer. can see 1 question here.


Comments