Wednesday, 11 November 2015

Store ArrayList in android SharedPreference

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));
}

No comments:

Post a Comment