android – 如何在SharedPreferences中保存arrayList数据?

前端之家收集整理的这篇文章主要介绍了android – 如何在SharedPreferences中保存arrayList数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参见英文答案 > Save ArrayList to SharedPreferences                                    31个
我在SharedPreferences中保存ArrayList大小和数据.当我从SharedPreference中检索ArrayList大小时,它会提供与之前保存的大小无关的确切大小.但是在这里我没有保存ArrayList数据.当我检索ArrayList数据时,它总是给出ArrayList的最后一项.

这是我保存ArrayList大小和数据的代码

  1. ArrayList

在这里,我正在检索我的ArrayList项:

  1. String getListSize = MyPreferences.savePreferences(getActivity(),"arraylist_size");
  2. System.out.println("arrayList size : " + getListSize);
  3. if (!getListSize.equals("")) {
  4. int listSize = Integer.parseInt(getListSize);
  5. if (listSize > 0) {
  6. for (int i = 0; i < listSize; i++) {
  7. String getListitems = MyPreferences
  8. .getPreferences(getActivity(),"id");
  9. System.out.Println("list items : "+ getListItems);
  10. }
  11. }
  12. }

如何在SharedPreferences中存储ArrayList项?

最佳答案
试试这个:

  1. //Set the values
  2. SetscoreEditor.putStringSet("key",set);
  3. scoreEditor.commit();
  4. //Retrieve the values
  5. Setscores.getStringSet("key",null);

这个问题已经在这里回答了Save ArrayList to SharedPreferences

  1. public void addTask(Task t) {
  2. if (null == currentTasks) {
  3. currentTasks = new ArrayList
  1. public void onCreate() {
  2. super.onCreate();
  3. if (null == currentTasks) {
  4. currentTasks = new ArrayList

在这里,您将任务类更改为itemclass.

猜你在找的Android相关文章