Skip to content Skip to sidebar Skip to footer

How To Save The Arraylist Data In Sharedpreferences?

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.

Solution 1:

Try this:

//Set the valuesSet<String>set=new HashSet<String>();
set.addAll(mArrayList1);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();

//Retrieve the valuesSet<String>set=new HashSet<String>();
set= myScores.getStringSet("key", null);

This question already answered here Save ArrayList to SharedPreferences

publicvoidaddTask(Task t) {
        if (null == currentTasks) {
            currentTasks = newArrayList<task>();
        }
        currentTasks.add(t);

        //save the task list to preferenceSharedPreferencesprefs= getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
        Editoreditor= prefs.edit();
        try {
            editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
        } catch (IOException e) {
            e.printStackTrace();
        }
        editor.commit();
    }

publicvoidonCreate() {
        super.onCreate();
        if (null == currentTasks) {
            currentTasks = newArrayList<task>();
        }

        //      load tasks from preferenceSharedPreferencesprefs= getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

        try {
            currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(newArrayList<task>())));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

Here you change the taskclass as itemclass.

Post a Comment for "How To Save The Arraylist Data In Sharedpreferences?"