I have an Arraylist of objects. Each time I press a button, I create an object and place it in the ArrayList. I need to save this array list when I exit the app. When I come and again press the button, it should add a new object to the existing array list saved to the Shared Preference,
I am not able to store and receive the array list. I used GSON but I am not very clear how it works.
Code Snippet:
public static ArrayList <BookMark> bkmarkList = new ArrayList();
static SharedPreferences keyvalues =
PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
static Editor editor = keyvalues.edit();
final Button bkmarkButton = (Button) view.findViewById(R.id.mark_button);
bkmarkButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bkmarkButton.setBackgroundResource(R.drawable.bookmarked);
bkmarkButton.setEnabled(false);
bkmarkButton.invalidate();
String wss = data.mWss;
String guid = data.mGuid;
String title = data.mTitle;
//SharedPreferences keyvalues = activity.getSharedPreferences("bookmark_list", Context.MODE_PRIVATE);
//bkmarkList = (ArrayList<BookMark>) ObjectSerializer.deserialize(keyvalues.getString(bookmark_list, ObjectSerializer.serialize(new ArrayList<BookMark>())));
//editor.getStringSet()
Gson gson = new Gson();
editor.putString("bookmark_list", new Gson().toJson(bkmarkList).toString());
if(keyvalues.getString("bookmark_list","")!=null);
{
ArrayList<BookMark> bkMark = new ArrayList<BookMark>();
//bkMark = gson.fromJson("bookmark_list", ArrayList<BookMark>);
//String value = keyvalues.getString("bookmark_list","");
//keyvalues.getStringSet(gson.fromJson(bkmarkList))
//bkmarkList = gson.fromJson(value, "");
}
bkmarkList.add(new BookMark(wss,guid, title));
}
});