I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences but I can't figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences isn't the way to go about this? Is there a simpler method?
当前回答
对于String, int, boolean,最好的选择是sharedPreferences。
如果你想存储数组列表或任何复杂的数据。最好的选择是Paper library。
添加依赖关系
implementation 'io.paperdb:paperdb:2.6'
初始化文件
应该在Application.onCreate()中初始化一次:
Paper.init(context);
Save
List<Person> contacts = ...
Paper.book().write("contacts", contacts);
加载数据
如果对象在存储中不存在,请使用默认值。
List<Person> contacts = Paper.book().read("contacts", new ArrayList<>());
给你。
https://github.com/pilgr/Paper
其他回答
使用Kotlin,对于简单的数组和列表,你可以这样做:
class MyPrefs(context: Context) {
val prefs = context.getSharedPreferences("x.y.z.PREFS_FILENAME", 0)
var listOfFloats: List<Float>
get() = prefs.getString("listOfFloats", "").split(",").map { it.toFloat() }
set(value) = prefs.edit().putString("listOfFloats", value.joinToString(",")).apply()
}
然后轻松访问首选项:
MyPrefs(context).listOfFloats = ....
val list = MyPrefs(context).listOfFloats
这就是你的完美解决方案。试一试,
public void saveArrayList(ArrayList<String> list, String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(key, json);
editor.apply(); // This line is IMPORTANT !!!
}
public ArrayList<String> getArrayList(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
return gson.fromJson(json, type);
}
public static void WriteSharePrefrence1(Context context, String key,
ArrayList<HashMap<String, String>> value)
{
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(value);
editor.putString(key, json);
editor.commit();
}
public static ArrayList<HashMap<String, String>> ReadSharePrefrence1(Context context,
String key)
{
String data;
Gson gson = new Gson();
ArrayList<HashMap<String, String>> items = new ArrayList<>();
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = preferences.edit();
data = preferences.getString(key, "");
Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {}.getType();
items = gson.fromJson(data, type);
return items;
}
对于String, int, boolean,最好的选择是sharedPreferences。
如果你想存储数组列表或任何复杂的数据。最好的选择是Paper library。
添加依赖关系
implementation 'io.paperdb:paperdb:2.6'
初始化文件
应该在Application.onCreate()中初始化一次:
Paper.init(context);
Save
List<Person> contacts = ...
Paper.book().write("contacts", contacts);
加载数据
如果对象在存储中不存在,请使用默认值。
List<Person> contacts = Paper.book().read("contacts", new ArrayList<>());
给你。
https://github.com/pilgr/Paper
我的utils类保存列表到SharedPreferences
public class SharedPrefApi {
private SharedPreferences sharedPreferences;
private Gson gson;
public SharedPrefApi(Context context, Gson gson) {
this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
this.gson = gson;
}
...
public <T> void putList(String key, List<T> list) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, gson.toJson(list));
editor.apply();
}
public <T> List<T> getList(String key, Class<T> clazz) {
Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();
return gson.fromJson(getString(key, null), typeOfT);
}
}
使用
// for save
sharedPrefApi.putList(SharedPrefApi.Key.USER_LIST, userList);
// for retrieve
List<User> userList = sharedPrefApi.getList(SharedPrefApi.Key.USER_LIST, User.class);
. 我的utils的完整代码//检查使用实例的活动代码
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。