Reputation: 29
in my android app I stored a list of a custom class List via gson in shared preferences. Now my problem is that I changed code in the class WORD and I have no idea how to apply this to the current list in "shared preferences" of the old version of my app. Thanks for the help!
Upvotes: 1
Views: 319
Reputation: 120
I had the same problem. my work around was to surround your error with a try catch
and create a new list of objects out of the old ones. Then save your newly created list so it doesnt happen again.
try {
if (object.getNewMethod().equals("")) {
wontHappenBecauseError();
}
}
catch (Exception e) {
//create new arraylist to save
ArrayList<Object> updatedObjectList = new ArrayList<>();
//loop through old objects
for(Object oldObject: oldObjectsList){
//for each, add an object to the new list from the attributes of the old list
updatedObjectsList.add(new Object(oldObject.getAttribute, oldObject.getOtherAttribute);
}
//save the new and improved list so it doesnt happen again
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Object>>(){}.getType();
String json = gson.toJson(updatedObjectList,type);
editor.putString("ObjectList",json);
editor.commit();
}
Upvotes: 1