Wednesday 23 July 2014

Store and Retrieve Array list using shared preferences in Android

Store Arraylist Using  Shared Preferences
------------------------------------------------------

SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
Editor edit=prefs.edit();

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist Name);
edit.putStringSet("yourKey", set);
edit.commit();

Retrieve Arraylist from Shared Preferences
-----------------------------------------------------

Set<String> set = prefs.getStringSet("yourKey", null);
List<String> sample=new ArrayList<String>(set);

9 comments:

  1. perfect answer. Its really helpful.

    ReplyDelete
  2. Can you provide the entire code.
    Where we have to add these line in on create or we have to create separate method

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. How can i store and retrieve ArrayList of LatLng (or any kind of object) to sharedpreferences?

    ReplyDelete
  5. How do i iterate through the list containing the items?

    ReplyDelete
  6. There is no method putIntSet("yourKey", set);
    Is there alternative for this?

    ReplyDelete
  7. Attempt to invoke interface method 'java.util.Set android.content.SharedPreferences.getStringSet(java.lang.String, java.util.Set)' on a null object reference

    ReplyDelete