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

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

问题描述

哦,那个男孩,所以我花了最近一个小时左右的时间阅读并试图存储 Parcelable Object 的 ArrayList code>在Android偏好设置和本地存储中,现在我又回到原点了。顺便说一下,我已经下载并导入了Gson,并不是说我可以让它工作。

基本上,我在 ArrayList<>中有一个名为 Task code>称为 taskList ,即使当我的用户关闭我的应用程序时,我也需要保存它。 Object 包含一个字符串,一些 Ints ,一些布尔值和一个类别子任务的ArrayList 。重要的是,我似乎不能把它写成一个字符串列表,我发现的所有教程都只显示保存简单的ArrayLists,比如字符串,它们都是可序列化的,而不是可分段的。



编辑:感谢您的建议,但我实现了Parcelable来包裹活动中列表中的特定对象..

解决方案

根据

在首选项中设置值:



  // MY_PREFS_NAME  - 静态字符串变量,如:
// public static final String MY_PREFS_NAME =MyPrefsFile;
SharedPreferences.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); 
字符串restoredText = prefs.getString(text,null);
if(restoredText!= null){
String name = prefs.getString(name,No name defined); //No name defined是默认值。
int idName = prefs.getInt(idName,0); // 0是默认值。

$ / code $ / pre

更多信息

使用共享首选项



共享首选项

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&lt;自定义对象&gt;到本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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