将 ArrayList 保存到 SharedPreferences [英] Save ArrayList to SharedPreferences

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

问题描述

我有一个带有自定义对象的 ArrayList.每个自定义对象都包含各种字符串和数字.即使用户离开活动然后想稍后回来,我也需要数组保持不变,但是在应用程序完全关闭后我不需要可用的数组.我通过使用 SharedPreferences 以这种方式保存了很多其他对象,但我不知道如何以这种方式保存我的整个数组.这可能吗?也许 SharedPreferences 不是解决这个问题的方法?有没有更简单的方法?

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?

推荐答案

API 11 之后,SharedPreferences Editor 接受 Sets.您可以将您的 List 转换为 HashSet 或类似的东西并像这样存储它.当您读回它时,将其转换为 ArrayList,如果需要,对其进行排序,您就可以开始使用了.

After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you 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,然后将其保存到/从 SharedPreferences 读取.解决方法如下:


You can also serialize your ArrayList and then save/read it to/from SharedPreferences. Below is the solution:


好的,下面是将ArrayList作为序列化对象保存到SharedPreferences然后从SharedPreferences中读取的解决方案.


Ok, below is the solution to save ArrayList as a serialized object to SharedPreferences and then read it from SharedPreferences.

因为 API 仅支持在 SharedPreferences 中存储和检索字符串(API 11 之后,它更简单),我们必须将具有任务列表的 ArrayList 对象序列化和反序列化为字符串.

Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, it's simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into a string.

在TaskManagerApplication类的addTask()方法中,我们要获取共享首选项的实例,然后使用putString()方法存储序列化的ArrayList:

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() 方法中的首选项中检索任务列表:

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 Pig 项目中获取 ObjectSerializerObjectSerializer.java

You can get the ObjectSerializer class from the Apache Pig project ObjectSerializer.java

这篇关于将 ArrayList 保存到 SharedPreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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