0

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

                }
            });
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Sweety Bertilla
  • 972
  • 10
  • 35

1 Answers1

0

GSON is very easy to use, look here: How Android SharedPreferences save/store object

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); To Save

  Editor prefsEditor = mPrefs.edit();
     Gson gson = new Gson();
     String json = gson.toJson(MyObject);
     prefsEditor.putString("MyObject", json);
     prefsEditor.commit();

To Retreive

  Gson gson = new Gson();
    String json = mPrefs.getString("MyObject", "");
    MyObject obj = gson.fromJson(json, MyObject.class);
Community
  • 1
  • 1
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • Instead of object - (MyObject obj) is there a way I can get the ArrayList or save the ArrayList – Sweety Bertilla Dec 30 '13 at 21:45
  • ArrayList list=(ArrayList)MyObject; When you save it you don't need any casts, because ArrayList is already an object. – Boldijar Paul Dec 30 '13 at 21:49
  • MyObject.class?? Do I need to create it, because by default its not taking it. my code: bkmarkList.add(new BookMark(wss,guid, title)); Gson gson = new Gson(); editor.putString("bookmark_list", gson.toJson(bkmarkList).toString()); editor.commit(); Gson gsonGet = new Gson(); String json = keyvalues.getString("bookmark_list", ""); MyObject listObject = gsonGet.fromJson(json, MyObject.class); ArrayList list = (ArrayList)listObject; – Sweety Bertilla Dec 31 '13 at 15:47
  • Fixed the type casting issue for Arraylist using the below link http://stackoverflow.com/questions/14673694/deserialize-json-string-with-gson – Sweety Bertilla Jan 02 '14 at 15:33
  • It does not work for objects that already have JSON objects nor ArrayList objects... – NaN Jun 06 '14 at 22:32