In this blog, we will explore the Store Array lists in Shared Preference Android by using two ways. so let’s get started.
For storing a list of objects (String used in this article or any other object) into Shared preference. first, we need to convert List<String> into a string. we easily convert a list of objects in Java or Kotlin into a JSON string using GSON.
1. Change the ArrayList into GSON
Gson is a Java library that can be used to convert Java Objects into their JSON representation and It can also be used to convert a JSON string to an equivalent Java object.
Before using GSON in our project, please add the below dependency to your app-level Gradle file.
1 2 3 4 |
dependencies { api 'com.google.code.gson:gson:2.9.0' } |
After adding a dependency, now we are creating an Array list in Android because we need an array list.
1 2 3 4 5 6 7 |
List<String> data = new ArrayList<String>(); //adding values to list data.add("Flutter"); data.add("Android"); data.add("iOS"); |
You can see the above code, we have created an array list and stored some string values. now we are Converting the List of objects into JSON string because we need to convert the array list into the string.
1 2 3 4 |
String convertedData = new Gson().toJson(data); // converted to string. |
In this code, we are converting our data array list into the string by using the GSON library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static final String PREF = "PREF"; private static final String KEY = "KEY_DATA"; public static SharedPreferences.Editor getSharedPreferenceEditor(Context context, String preferenceFile) { return context.getSharedPreferences(preferenceFile,MODE_PRIVATE).edit(); } // put convertedData into shared preference getSharedPreferenceEditor(context,PREF) .putString(KEY,convertedData).apply(); |
We are creating a Shared Preference Editor and storing our converted data into Shared Preference.
1 2 3 4 |
String retrievedDataString= getSharedPreference(context,PREF).getString(KEY, null); |
Accessing stored JSON string from shared preference as List of Object.
1 2 3 4 5 6 7 8 |
List<String> retrievedDataInList = Type type = new TypeToken<List<String>>() { }.getType(); return new Gson().fromJson(retrievedDataString, type); |
Here, type is used to tell Gson that we want this JSON to be translated to a List of Strings.
2. Change the ArrayList into HashSet
You might save your list in a hash set or a similar format after converting it. After reading it back, you can transform it into an ArrayList, arrange it as necessary, and you’re set to go.
1 2 3 4 5 6 7 8 9 |
//Set the values Set<String> dataSet = new HashSet<String>(); dataSet.addAll(data); // put convertedData into shared preference getSharedPreferenceEditor(context,PREF) .putStringSet(KEY,dataSet).apply(); |
In the above code, we are changing our Array list into the HashSet and storing the shared preference.
1 2 3 4 5 |
Set<String> retreiveSetData = getSharedPreference(context,PREF).getStringSet(KEY, null); |
Thanks for reading this blog, you can also check other blogs from here for more knowledge.
Happy Coding and always welcome your feedback.