存储自定义对象的数组列表 [英] Store Arraylist of custom objects

查看:110
本文介绍了存储自定义对象的数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在共享首选项中保存了自定义对象的ArrayList,如下所示:

I have saved ArrayList of custom objects in the shared preferences like this:

SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putStringSet("myList", new Gson().toJson(arraylist).toString());
editor.apply();  

这样做,一旦保存了值,但是当我退出应用程序并重新启动并尝试保存新值时,旧值就消失了.
知道如何保存旧值并附加新值吗?
我想将所有值保存在同一数组中,并保存数组以在每次加载应用程序时显示值.

By doing this, once values are saved but when i exit from the app and relaunch and try to save new values then the old values are gone.
Any idea how can i save old values and append new ones?
I want to keep all values in the same array and keep array saved to show values every time when the app loads.

推荐答案

prefName读取myList,将arraylist附加到首选项中已保存的内容,再将myList写回首选项.

Read myList from prefName, append arraylist to what was already saved in the preferences, write back myList to preferences.

这篇关于存储自定义对象的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆