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

                }
            });
1
  • this also looks like java? Please tag accordingly. Commented Dec 30, 2013 at 21:40

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of object - (MyObject obj) is there a way I can get the ArrayList or save the ArrayList
ArrayList<BookMark> list=(ArrayList<BookMark>)MyObject; When you save it you don't need any casts, because ArrayList<type> is already an object.
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<BookMark> list = (ArrayList<BookMark>)listObject;
Fixed the type casting issue for Arraylist using the below link stackoverflow.com/questions/14673694/…
It does not work for objects that already have JSON objects nor ArrayList objects...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.