ArrayList中保存到共享preferences [英] Save ArrayList to SharedPreferences
问题描述
我有一个的ArrayList
自定义对象。每个自定义对象包含了各种字符串和数字。予需要该阵列即使用户离开活动,然后想回来在以后的时间,要坚持,但是我不需要后可用的应用程序已经完全关闭的阵列。我节省了很多其他物体的这种方式使用共享preferences
,但我无法弄清楚如何拯救我的整个数组这种方式。这可能吗?也许共享preferences
不是办法去了解呢?是否有一个更简单的方法?
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?
推荐答案
11 API共享preferences编辑器接受设置之后。您可以将您的列表到HashSet的或类似的东西,并将其存储这样的。当你读回,将其转换成一个ArrayList,排序,如果需要,你是好去。
After API 11 the SharedPreferences Editor accept Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList, sort it if needed and you're good to go.
//Retrieve the values
Set<String> set = myScores.getStringSet("key", null);
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
您也可以序列化的ArrayList,然后保存/从共享preferences读/。下面是解决方案:
You can also serialize your ArrayList and then save/read it to/from SharedPreferences. Below is the solution:
编辑:确定,下面是解决方案,以节省ArrayList的系列化对象共享preferences,然后从共享preferences阅读
Ok, below is the solution to save ArrayList as serialized object to SharedPreferences and then read it from SharedPreferences.
由于API仅支持存储和检索字符串/从共享preferences的(API 11后,它的简单),我们必须序列化和反序列化ArrayList的对象,它有任务列表转换为字符串。
Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, its simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into string.
在TaskManagerApplication类的 addTask()
方法,我们得到的共享preference的实例,然后使用存储序列化的ArrayList putString()
方法:
In the addTask()
method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using the putString()
method:
public void addTask(Task t) {
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}
currentTasks.add(t);
//save the task list to preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
同样,我们需要检索从的onCreate()
方法preference任务列表:
Similarly we have to retrieve the list of tasks from the preference in the onCreate()
method:
public void onCreate() {
super.onCreate();
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}
// load tasks from preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
try {
currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
您可以从Apache的猪项目<一大家子ObjectSerializer类href="https://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/ObjectSerializer.java">ObjectSerializer.java
You can get ObjectSerializer class from Apache Pig project ObjectSerializer.java
这篇关于ArrayList中保存到共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!