本文介绍了如何在 SharedPreferences 中保存 arrayList 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ArrayList 大小和数据保存在 SharedPreferences 中.当我从 SharedPreference 检索 ArrayList 大小时,它会给出确切的大小,无论之前保存的大小如何.但是在这里我未能保存 ArrayList 数据.当我检索 ArrayList 数据时,它总是给出 ArrayList 的最后一项.

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大小和数据的代码:

Here is my code for saving ArrayList size and data :

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);
            }
}

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

Here I am retrieving my ArrayList items :

  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);

            }
        }
    }

如何将 ArrayList 项存储在 SharedPreferences 中?

How can I store the ArrayList items in SharedPreferences?

推荐答案

试试这个:

//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);

此问题已在此处回答将 ArrayList 保存到 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.

这篇关于如何在 SharedPreferences 中保存 arrayList 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:00