Storing a Data List in Phimpme Android
In Phimpme Android, it is required to store all the available camera parameters like a list of ISO values, available camera resolution etc. so that it can be displayed to the user in the camera settings. In Phimpme, we have stored these list of data in SharedPreferences with some modifications. As we cannot store a list directly in SharedPreference, in this post I will be discussing how we achieved this in Phimpme Android application.
To store the ArrayList you have to create a function that will convert the array into a string by using some symbol.
Step – 1
First, Create a class say TinyDB which contains functions to store an array in sharedPreferences.
public class TinyDB { private SharedPreferences preferences; public TinyDB(Context appContext) { preferences = PreferenceManager.getDefaultSharedPreferences(appContext); } } |
Step – 2
Create functions to convert the array into string and store in sharedPreferences.
putListInt() method will convert the string ArrayList to String and store in sharedPreferences.
Similarly, putListString() method will convert the integer ArrayList to string and store in sharedPreferences.
public void putListInt(String key, ArrayList<Integer> intList) { if (key == null) return; if (intList==null) return; Integer[] myIntList = intList.toArray(new Integer[intList.size()]); preferences.edit().putString(key, TextUtils.join(“‚‗‚”, myIntList)).apply(); } public void putListString(String key, ArrayList<String> stringList) { if (key == null) return; if (stringList ==null)return; String[] myStringList = stringList.toArray(new String[stringList.size()]); preferences.edit().putString(key, TextUtils.join(“‚‗‚”, myStringList)).apply(); }
|
Now create the object of TinyDB.class to call the above functions using tinyDb object.
Now our data is saved in sharedPreference to get this data we have to create a getter for the ArrayList.
Step-3
Add two functions in TinyDB.class to get the string and integer ArrayList.
public ArrayList<String> getListString(String key) { return new ArrayList<String>(Arrays.asList(TextUtils.split(preferences. =getString(key, “”), “‚‗‚”))); } public ArrayList<Integer> getListInt(String key) { String[] myList = TextUtils.split(preferences.getString(key, “”), “‚‗‚”); ArrayList<String> arrayToList = new ArrayList<String>(Arrays.asList(myList)); ArrayList<Integer> newList = new ArrayList<Integer>(); for (String item : arrayToList) newList.add(Integer.parseInt(item)); return newList; } |
Now to get the saved integer and string ArrayList simply call this function by creating an instance of TinyDB.class.
The below screenshot depicts how we have stored the list of camera resolutions in SharedPreference using TinyDB class.
So this is how you can store the entire ArrayList in sharedPreferences. For more detail, you can see the TinyDb.class in our Phimpme project.
Resources:
https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences
http://blog.nkdroidsolutions.com/arraylist-in-sharedpreferences/
You must be logged in to post a comment.