在我的Android应用程序中,我通过gson在共享首选项中存储了自定义类List的列表。现在我的问题是我更改了WORD类中的代码,却不知道如何将其应用于旧版本应用程序的“共享首选项”中的当前列表。谢谢您的帮助!
我有同样的问题。我的解决方法是用a包围您的错误,try catch
并从旧对象中创建一个新的对象列表。然后保存您新创建的列表,以免再次发生。
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句