Android 正在更新共享首选项中的列表
在我的android应用程序中,我通过gson在共享首选项中存储了一个自定义类列表。Android 正在更新共享首选项中的列表,android,gson,sharedpreferences,Android,Gson,Sharedpreferences,在我的android应用程序中,我通过gson在共享首选项中存储了一个自定义类列表。 现在我的问题是,我更改了WORD类中的代码,我不知道如何将其应用到旧版本应用程序的“共享首选项”中的当前列表。谢谢你的帮助 我也有同样的问题。我的工作是用一个try catch来包围您的错误,并用旧对象创建一个新的对象列表。然后保存新创建的列表,这样它就不会再次发生 try { if (object.getNew
现在我的问题是,我更改了WORD类中的代码,我不知道如何将其应用到旧版本应用程序的“共享首选项”中的当前列表。谢谢你的帮助 我也有同样的问题。我的工作是用一个
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();
}
试试看{
if(object.getNewMethod().equals(“”){
wontHappenBecauseError();
}
}
捕获(例外e){
//创建要保存的新arraylist
ArrayList UpdateObjectList=新建ArrayList();
//循环浏览旧对象
用于(对象oldObject:oldObjectsList){
//对于每个对象,从旧列表的属性向新列表添加一个对象
UpdateObjectsList.add(新对象(oldObject.getAttribute,oldObject.getOtherAttribute);
}
//保存新的和改进的列表,使其不再发生
Gson Gson=新的Gson();
Type Type=new-TypeToken(){}.getType();
字符串json=gson.toJson(updateObjectList,type);
putString(“ObjectList”,json);
commit();
}
您是否可以编辑您的帖子以包含相关代码和您尝试过的内容?