Java 反序列化自定义对象的序列化ArrayList,添加对象,然后重新序列化
我正在尝试为我正在使用android Studio制作的应用程序存储一个自定义的对象数组列表。我需要能够在用户按下按钮时向列表中添加新对象。我的方法是首先用正确的类型(try/catch的catch)初始化数组列表的空序列化版本。然后将该数组反序列化为名为“RecoTrackGameCollection”的临时数组列表,然后添加新对象,重新序列化数组并保存它 我遇到的问题是,当我尝试向“RecoTrackGameCollection”添加任何对象时,代码失败并运行catch 谢谢你花时间看这个。如果您还需要其他信息,请告诉我Java 反序列化自定义对象的序列化ArrayList,添加对象,然后重新序列化,java,android,arraylist,gson,sharedpreferences,Java,Android,Arraylist,Gson,Sharedpreferences,我正在尝试为我正在使用android Studio制作的应用程序存储一个自定义的对象数组列表。我需要能够在用户按下按钮时向列表中添加新对象。我的方法是首先用正确的类型(try/catch的catch)初始化数组列表的空序列化版本。然后将该数组反序列化为名为“RecoTrackGameCollection”的临时数组列表,然后添加新对象,重新序列化数组并保存它 我遇到的问题是,当我尝试向“RecoTrackGameCollection”添加任何对象时,代码失败并运行catch 谢谢你花时间看这个。
try {
//get shared pref
SharedPreferences prefs = mContext.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
//deserilize
Gson gson = new Gson();
String serialRecoverList = prefs.getString("SavedGames", "");
Log.wtf("String Recover", serialRecoverList);
Type type = new TypeToken<List<Game>>(){}.getType();
ArrayList<Game> RecoTrackGameCollection = gson.fromJson(serialRecoverList, type);
//add game
RecoTrackGameCollection.add(SearchGameCollection.get(position));
//reserilize
Gson NewGson = new Gson();
String JsonTrakingGames = NewGson.toJson(RecoTrackGameCollection);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Games", JsonTrakingGames);
editor.commit();
Toast.makeText(mContext , "Game Saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Gson gson = new Gson();
String JsonTrakingGames = gson.toJson(TrackGameCollection);
SharedPreferences prefs = mContext.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Games", JsonTrakingGames);
editor.commit();
Toast.makeText(mContext , "iniatlizing", Toast.LENGTH_LONG).show();
}
我认为我的错误在于数组的反序列化。特别是这一行:
ArrayList<Game> RecoTrackGameCollection = gson.fromJson(serialRecoverList,
type);
ArrayList RecoTrackGameCollection=gson.fromJson(serialRecoverList,
类型);
这是因为在保存和获取列表时使用了不同的键
您可以使用以下命令保存列表:
private void saveGames(Lis<Game> games) {
Gson gson = new Gson();
String json = gson.toJson(games);
SharedPreferences prefs = ctx.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Games", json);
editor.commit();
}
private List<Game> getGames(Context ctx) {
Gson gson = new Gson();
SharedPreferences prefs = ctx.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
String json = prefs.getString("Games", "");
if(json.isEmpty()) {
return new ArrayList<>();
} else {
Type type = new TypeToken<List<Game>>(){}.getType();
return gson.fromJson(json, type);
}
}
private void saveGames(Lis游戏){
Gson Gson=新的Gson();
字符串json=gson.toJson(游戏);
SharedReferences prefs=ctx.getSharedReferences(“SavedGames”,Context.MODE_PRIVATE);
SharedReferences.Editor=prefs.edit();
putString(“Games”,json);
commit();
}
要获取列表,请执行以下操作:
private void saveGames(Lis<Game> games) {
Gson gson = new Gson();
String json = gson.toJson(games);
SharedPreferences prefs = ctx.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Games", json);
editor.commit();
}
private List<Game> getGames(Context ctx) {
Gson gson = new Gson();
SharedPreferences prefs = ctx.getSharedPreferences("SavedGames", Context.MODE_PRIVATE);
String json = prefs.getString("Games", "");
if(json.isEmpty()) {
return new ArrayList<>();
} else {
Type type = new TypeToken<List<Game>>(){}.getType();
return gson.fromJson(json, type);
}
}
私有列表getGames(上下文ctx){
Gson Gson=新的Gson();
SharedReferences prefs=ctx.getSharedReferences(“SavedGames”,Context.MODE_PRIVATE);
String json=prefs.getString(“游戏”,“游戏”);
if(json.isEmpty()){
返回新的ArrayList();
}否则{
Type Type=new-TypeToken(){}.getType();
返回gson.fromJson(json,类型);
}
}
请阅读“如何创建一个应用程序”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并帮助您。例如:什么是异常堆栈跟踪?提示:了解类型擦除。这个空列表不知道它应该包含哪种类型的对象@幽灵猫会的,再次谢谢你:请阅读。你有什么例外?添加堆栈跟踪@GhostCat我有解决方案,所以我不会编辑这个问题,但我会确保下次做对。谢谢你的努力。@GhostCat:谢谢你!我以前从未意识到这一点。在进一步阅读Gson文档后,您完全正确。我将更新答案。这解决了问题,非常感谢您的支持。