在共享的首选项中保存两种不同类型的阵列列表
我目前有一个特定类型的阵列列表,我正在尝试将其保存在共享的首选中。
我有一个叫温克拉斯的课。我有名为Loggedinwin和Userwin的课程,都继承了Winclass类。 有一个具有两种类型的对象的Arraylist winclasses。 我目前有一个代码,可以成功地将ArrayList保存在共享的首选中,但仅保存Winclass对象。 这意味着,如果我的arraylist有winclass,loggedinwin和userwin的项目,则只会保存winclass的对象。 我已经看了看和我的代码,并更改了将数据类型加载以仅加载logggedinwin对象的部分。这一次它仅保存了loggedinwin对象。 这让我想知道如何将其保存的所有对象保存(包括所有类型)保存。
我保存ArrayList的代码:
private void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(winClasses);
editor.putString("task list", json);
editor.apply();
}
private void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<LoggedInWin>>() {}.getType();
winClasses = gson.fromJson(json, type);
if (winClasses == null) {
winClasses = new ArrayList<>();
}
}
I currently have an arraylist of a certain type which I'm trying to save in shared prefernces.
I have a class called WinClass. I have classes called LoggedInWin and UserWin, both inheriting the WinClass class.
There's an arraylist winClasses which has objects of both types.
I currently have a code that saves the ArrayList in shared prefernces successfully, but only saves WinClass objects.
That means that if my Arraylist has items of Winclass, LoggedInWin and UserWin, it will only save the objects of WinClass.
I have looked and my code and changed the part where it loads the data type to load only objects of LoggedInWin. It has in-fact this time only saved LoggedInWin objects.
That left me wondering how can I have the arraylist saved with all of its objects saved with it (all types included).
The code where I save the arraylist:
private void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(winClasses);
editor.putString("task list", json);
editor.apply();
}
private void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<LoggedInWin>>() {}.getType();
winClasses = gson.fromJson(json, type);
if (winClasses == null) {
winClasses = new ArrayList<>();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终解决了它。
这是(以防万一需要它)的方式
保存了一种类型的阵列列表(loggedinwin)
保存了其他类型的Anoher Arraylist(UserWin)
将他们俩都放在第三个Arraylist(Winclass)中。
然后,Winclass Arraylist保存了。
非常简单的解决方案,但没有立即考虑
Ended up solving it.
Here's how (in case anyone needs it in the future)
Saved one Arraylist of one type (LoggedInWin)
Saved anoher Arraylist of the other type (UserWin)
Put them both in a third arraylist (WinClass).
Then the WinClass arraylist was saved.
Pretty simple solution but didn't think about it straight away