如何在SharedPreferences中保存arrayList数据?

时间:2014-02-15 06:55:06

标签: android arraylist sharedpreferences

我正在ArrayList中保存SharedPreferences尺寸和数据。当我从ArrayList检索SharedPreference大小时,它会提供与之前保存的大小无关的确切大小。但在这里我没有保存ArrayList数据。当我检索ArrayList数据时,它始终提供ArrayList的最后一项。

以下是我保存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);
            }
}

我在这里检索我的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);

            }
        }
    }

如何将ArrayList项目存储在SharedPreferences

1 个答案:

答案 0 :(得分:4)

试试这个:

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

这个问题已在此处回答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();
        }
    }

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