Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the arrayList data in SharedPreferences? [duplicate]

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.

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

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

            }
        }
    }

How can I store the ArrayList items in SharedPreferences?

like image 512
Android_dev Avatar asked Oct 01 '22 14:10

Android_dev


1 Answers

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

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.

like image 114
RVG Avatar answered Oct 07 '22 18:10

RVG