I am working on a small Android App!
I have a class MyPref
public class MyPref{
{
java.util.Calendar cal;
int id;
String name;
//some more methods and constructor
}
I need to store an array of MyPref objects to sharedpreferences…
java.util.ArrayList<MyPref> array=new java.util.ArrayList<MyPref>();
array.add(//MyPref object);
array.add(//MyPref object);
now how can I store this ArrayList object to sharedpreferences..
I tried..
JSONObject o=new JSONObject();
o.put("list",array);
SharedPreferences s=PreferenceManager.getDefaultSharedPreferences(this);
Editor edt=s.edit();
edt.putString("mylist",o.toString());
edt.commit();
but this doesn’t worked!
is there any other alternative??
plz help me..
thanx in advace..
I may be wrong but I’m pretty sure you can’t use
JSONObject.put(...)to put an arbitrary Java object into aJSONObject. From the docs…There’s no mention of being able to put a Java object of any other type and you’re trying to put an
ArrayList<MyPref>.