保存 ArrayList<自定义对象>到本地存储 [英] Save ArrayList&lt;Custom Object&gt; to local storage

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

问题描述

哦,天哪,所以我花了大约一个小时阅读并尝试在 Android 首选项和本地存储中存储 Parcelable ObjectArrayList 的不同方法,现在我又回到了原点.顺便说一下,我已经下载并导入了 Gson,并不是说我可以让它工作.

基本上,我在名为 taskListArrayList<> 中有一个名为 Task 的类,即使我的用户关闭我也需要保存它我的应用程序.Object 由一个 String、一些 Ints、一些 Booleans 和一个 ArrayList 组成类 Subtask 如果这很重要.重要的是,我似乎不能把它写成一个字符串列表,而且我发现的所有教程都只展示了如何保存简单的 ArrayList,比如字符串,而且它们都是 Serializable,而不是 Parcelable.

感谢您的建议,但我实施 Parcelable 以在活动之间打包列表中的特定对象..

解决方案

根据

在首选项中设置值:

//MY_PREFS_NAME - 一个静态字符串变量,如://public static final String MY_PREFS_NAME = "MyPrefsFile";SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();editor.putString("name", "Elena");editor.putInt("idName", 12);editor.commit();

从偏好中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);String RestoreText = prefs.getString("text", null);如果(恢复文本!= null){String name = prefs.getString("name", "No name defined");//"No name defined"为默认值.int idName = prefs.getInt("idName", 0);//0 是默认值.}

更多信息:

使用共享首选项

共享偏好

Oh boy, so I've spent the last hour or so reading and trying different ways of storing an ArrayList of a Parcelable Object in the Android preferences and Local Storage, and now I'm back to square one. I have downloaded and imported Gson, by the way, not that I could get it to work.

Basically, I have a class called Task in an ArrayList<> called taskList that I need to save even when my users close my app. The Object consists of a String, some Ints, some Booleans and an ArrayList of class Subtask if that matters. What's important is that it seems I can't just write it as a list of Strings, and all the tutorials I've found only show saving simple ArrayLists like Strings and they have all been Serializable, not Parcelable.

EDIT: thanks for the suggestions, but I implement Parcelable to parcel specific objects in the list between activities..

解决方案

According to Parcelable API,

Parcelable: Interface for classes whose instances can be written to and restored from a Parcel.

And reading in Parcel API:

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

So if you want to store all the references of different types by an ArrayList you must wrap all the objects to a common interface, in this case Serializable is your friend.

Also, according your question:

I need to save even when my users close my app.

If you check the android lifecycle, you will see you need to perform this action in onStop(), which is called when your activity is no longer visible to the user.

ADDITIONAL INFO from this question

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

more info:

Using Shared Preferences

Shared Preferences

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

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