如何保存在共享preferences ArrayList中的数据? [英] How to save the arrayList data in SharedPreferences?

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

问题描述

我现在的储蓄ArrayList的大小和共享preference.When数据我正在检索从共享preference它给什么都节省大小previous.But在这里,我没有确切的尺寸大小的ArrayList在节能ArrayList的数据。当我正在检索ArrayList中的数据,它总是给人ArrayList的最后一个项目。

I am saving arraylist size and data in sharedPreference.When i am retrieving the arraylist size from sharedpreference it giving the exact size what ever 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.

下面是我的code保存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);

            }
        }
    }

这是我的code有什么错在我的code我怎么可以存储在共享preference的arraylistitems任何一个请指导我...

This is my code what wrong in my code how can i store the arraylistitems in sharedpreference any one please guide me...

推荐答案

试试这个。

//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保存到共享preferences

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

这里u改变taskclass作为itemClass时。

here u change the taskclass as itemclass.

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

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