How To Store And Retrieve ArrayList In SharedPreferences

Please follow and like us:

arraylist in sharedpreference

In Android Application Development there are plenty of options available for storing data in android, but for storing preference related settings can be easily stored and retrieved using sharedPreference class. In this tutorial we are going to store custom arraylist in sharedpreferences. Here we convert arraylist data to json format using Gson and save it as string. During retrieving we convert json string into arraylist.

How To Store And Retrieve ArrayList In SharedPreferences

Arraylist In SharedPreference Android Example

Let’s see an example of storing favorites item in sharedpreferences.

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SharedPreference {
    public static final String PREFS_NAME = "NKDROID_APP";
    public static final String FAVORITES = "Favorite";
    public SharedPreference() {
        super();
    }
public void storeFavorites(Context context, List favorites) {
// used for store arrayList in json format
        SharedPreferences settings;
        Editor editor;
        settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
        editor = settings.edit();
        Gson gson = new Gson();
        String jsonFavorites = gson.toJson(favorites);
        editor.putString(FAVORITES, jsonFavorites);
        editor.commit();
    }
    public ArrayList loadFavorites(Context context) {
// used for retrieving arraylist from json formatted string
        SharedPreferences settings;
        List favorites;
        settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
        if (settings.contains(FAVORITES)) {
            String jsonFavorites = settings.getString(FAVORITES, null);
            Gson gson = new Gson();
            BeanSampleList[] favoriteItems = gson.fromJson(jsonFavorites,BeanSampleList[].class);
            favorites = Arrays.asList(favoriteItems);
            favorites = new ArrayList(favorites);
        } else
            return null;
        return (ArrayList) favorites;
    }
    public void addFavorite(Context context, BeanSampleList beanSampleList) {
        List favorites = loadFavorites(context);
        if (favorites == null)
            favorites = new ArrayList();
        favorites.add(beanSampleList);
        storeFavorites(context, favorites);
    }
    public void removeFavorite(Context context, BeanSampleList beanSampleList) {
        ArrayList favorites = loadFavorites(context);
        if (favorites != null) {
            favorites.remove(beanSampleList);
            storeFavorites(context, favorites);
        }
    }
}

Now create new object of SharedPreference class and call addfavourite() and removeFavourite() for adding and removing object and call loadfavourites() for list of objects.

Check our new tutorials also:

Store And Retrieve Class Object In SharedPreferences

Android Push Notifications

Integrating Google Admob in android app

Android Google Analytics Example Integration

Hope you like the simple example of store ArrayList in Shared preference. Stay connected for latest android development tutorials for beginners.

nirav kalola