5

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?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Android_dev
  • 320
  • 1
  • 7
  • 26
  • Refer this [save-arraylist-to-sharedpreferences](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Chirag Ghori Feb 15 '14 at 06:59
  • refer this http://stackoverflow.com/questions/12150597/save-an-arraylist-of-strings-to-shared-preferences – rajshree Feb 15 '14 at 07:00

1 Answers1

4

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
RVG
  • 3,538
  • 4
  • 37
  • 64
  • thanks for giving reply.when i am adding this code set.addAll(mArrayList1); here i am getting The method addAll(Collection extends String>) in the type Set is not applicable for the arguments (ArrayList) this error.My arrylist extends Items class..How can i add my classtype arraylist into stringtypy arraylist. – Android_dev Feb 15 '14 at 07:57
  • User Set set = new HastSet() – RVG Feb 15 '14 at 08:32
  • This putStringset is not supporting for bellow Api level 11.How can i save my arraylist data into sharedpreference below api 11. – Android_dev Feb 15 '14 at 08:33
  • has your item class implement serializable ? i.e public class items implements Serializable{} – RVG Feb 15 '14 at 11:16
  • pls check the updated ans – RVG Feb 15 '14 at 11:22