Here I am going to explain how to store arraylist in sharedpreference.
Let's say I have an arraylist as following.
ArrayList<String> myArrayList=new ArrayList<String>();
myArrayList.add("value1");
myArrayList.add("value2");
myArrayList.add("value3");
myArrayList.add("value4");
myArrayList.add("value5");
myArrayList.add("value6");
Store arraylist in sharedpreference
SharedPreference sPrefs=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreference.Editor sEdit=sPrefs.edit();
for(int i=0;i<myArrayList.size();i++)
{
sEdit.putString("val"+i,myArrayList.get(i);
}
sEdit.putInt("size",myArrayList.size());
sEdit.commit();
Retrive arraylist from sharedpreference
I am retriving values in another arraylist
ArrayList<String> myAList=new ArrayList<String>();
int size=sPrefs.getInt("size",0);
for(int j=0;j<size;j++)
{
myAList.add(sPrefs.getString("val"+j));
}
Let's say I have an arraylist as following.
ArrayList<String> myArrayList=new ArrayList<String>();
myArrayList.add("value1");
myArrayList.add("value2");
myArrayList.add("value3");
myArrayList.add("value4");
myArrayList.add("value5");
myArrayList.add("value6");
Store arraylist in sharedpreference
SharedPreference sPrefs=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreference.Editor sEdit=sPrefs.edit();
for(int i=0;i<myArrayList.size();i++)
{
sEdit.putString("val"+i,myArrayList.get(i);
}
sEdit.putInt("size",myArrayList.size());
sEdit.commit();
Retrive arraylist from sharedpreference
I am retriving values in another arraylist
ArrayList<String> myAList=new ArrayList<String>();
int size=sPrefs.getInt("size",0);
for(int j=0;j<size;j++)
{
myAList.add(sPrefs.getString("val"+j));
}
Nice coding sir.
ReplyDeleteThanks !!
DeleteThank you ....
ReplyDeleteThank you!! Now how to delete an article array preferences?
ReplyDeleteWhat is your exact problem ?
DeleteThank you!! Now how to delete an article array preferences?
ReplyDeletemyAList.add(sPrefs.getString("val"+j ));
ReplyDeleteThe above line shows error.
What's the exact error?
DeleteThis comment has been removed by the author.
ReplyDeletehow can we store integer type data in list?
ReplyDeleteDefine your arraylist of type Integer
DeleteArrayList<Integer> myArrayList=new ArrayList<Integer>();
Store in sharedpreference
sEdit.putInt("val");
Retrieve from sharedpreference
sPrefs.getInt("val");
sir please tell me how can i store multi value array list in shared preferences?
DeleteSir how to implement SharedPreference for 2D arraylist or multdimentianal arraylist
ReplyDeleteThanks for sharing such an amazing post. Your style of writing is very unique. It made me mesmerized in your words. Keep on writing. c Software Testing Training in Chennai | Software Testing Training in Anna Nagar | Software Testing Training in OMR | Software Testing Training in Porur | Software Testing Training in Tambaram | Software Testing Training in Velachery
ReplyDeleteIt share more useful informative content and interrested your article.
ReplyDeletePython Training in Chennai
Python Training in Training
Python Training in Bangalore
Python Hyderabad
Python Training in Coimbatore