Save ArrayList to SharedPreferences Save ArrayList to SharedPreferences android android

Save ArrayList to SharedPreferences


After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you read it back, convert it into an ArrayList, sort it if needed and you're good to go.

//Retrieve the valuesSet<String> set = myScores.getStringSet("key", null);//Set the valuesSet<String> set = new HashSet<String>();set.addAll(listOfExistingScores);scoreEditor.putStringSet("key", set);scoreEditor.commit();

You can also serialize your ArrayList and then save/read it to/from SharedPreferences. Below is the solution:

EDIT:
Ok, below is the solution to save ArrayList as a serialized object to SharedPreferences and then read it from SharedPreferences.

Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, it's simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into a string.

In the addTask() method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using the putString() method:

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

Similarly we have to retrieve the list of tasks from the preference in the onCreate() method:

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

You can get the ObjectSerializer class from the Apache Pig project ObjectSerializer.java


Using this object --> TinyDB--Android-Shared-Preferences-Turbo its very simple.

TinyDB tinydb = new TinyDB(context);

to put

tinydb.putList("MyUsers", mUsersArray);

to get

tinydb.getList("MyUsers");

UPDATE

Some useful examples and troubleshooting might be found here: Android Shared Preference TinyDB putListObject frunction


Saving Array in SharedPreferences:

public static boolean saveArray(){    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);    SharedPreferences.Editor mEdit1 = sp.edit();    /* sKey is an array */    mEdit1.putInt("Status_size", sKey.size());      for(int i=0;i<sKey.size();i++)      {        mEdit1.remove("Status_" + i);        mEdit1.putString("Status_" + i, sKey.get(i));      }    return mEdit1.commit();     }

Loading Array Data from SharedPreferences

public static void loadArray(Context mContext){      SharedPreferences mSharedPreference1 =   PreferenceManager.getDefaultSharedPreferences(mContext);    sKey.clear();    int size = mSharedPreference1.getInt("Status_size", 0);      for(int i=0;i<size;i++)     {     sKey.add(mSharedPreference1.getString("Status_" + i, null));    }}