Android 如何在 SharedPreferences 中保存 arrayList 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21794419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save the arrayList data in SharedPreferences?
提问by Android_dev
I am saving ArrayList
size and data in SharedPreferences
. When I am retrieving the ArrayList
size from SharedPreference
it giving the exact size whatever the size saving previous. But here I am failing in Saving ArrayList
data. When I am retrieving the ArrayList
data it always giving last item of ArrayList
.
我将ArrayList
大小和数据保存在SharedPreferences
. 当我ArrayList
从中检索大小时,SharedPreference
无论之前保存的大小如何,都会给出确切的大小。但是在这里我在保存ArrayList
数据方面失败了。当我检索ArrayList
数据时,它总是给出ArrayList
.
Here is my code for saving ArrayList
size and data :
这是我保存ArrayList
大小和数据的代码:
ArrayList<Items> mArrayList1 = new ArrayList<Items>();
if(mArrayList1 == 0){
mStoreItem.setItem(id);
mArrayList1.add(mStoreItem);
int size = mArrayList1.size();
System.out.println("array list size : " + size);
MyPreferences.savePreferences(getActivity(),
"arraylist_size", Integer.toString(size));
for (int i = 0; i < mArrayList1.size(); i++) {
String id = mArrayList1.get(i)
.getItem();
MyPreferences.savePreferences(getActivity(),
"id", id);
}
} else if(mArrayList1 > 0){
mStoreItem.setItem(id);
mArrayList1.add(mStoreItem);
int size = mArrayList1.size();
System.out.println("arrayList size : " + size);
MyPreferences.savePreferences(getActivity(),
"arraylist_size", Integer.toString(size));
for (int i = 0; i < mArrayList1.size(); i++) {
String id = mArrayList1.get(i)
.getItem();
MyPreferences.savePreferences(getActivity(),
"id", id);
}
}
Here I am retrieving my ArrayList
items :
我在这里取回我的ArrayList
物品:
String getListSize = MyPreferences.savePreferences(getActivity(),
"arraylist_size");
System.out.println("arrayList size : " + getListSize);
if (!getListSize.equals("")) {
int listSize = Integer.parseInt(getListSize);
if (listSize > 0) {
for (int i = 0; i < listSize; i++) {
String getListitems = MyPreferences
.getPreferences(getActivity(), "id");
System.out.Println("list items : "+ getListItems);
}
}
}
How can I store the ArrayList
items in SharedPreferences
?
我该如何存放ArrayList
物品SharedPreferences
?
采纳答案by RVG
Try this:
尝试这个:
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(mArrayList1);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
//Retrieve the values
Set<String> set = new HashSet<String>();
set = myScores.getStringSet("key", null);
This question already answered here Save ArrayList to SharedPreferences
这个问题已经在这里回答Save ArrayList to SharedPreferences
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();
}
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();
}
}
Here you change the taskclass as itemclass.
在这里,您将任务类别更改为项目类别。