1

I saw similar questions in stackoverflow ( LINK , LINK ) and other websites . They are doing everything from an Activity hence they didn't get problem.

I have an Activity and Fragment class. I am trying to save ArrayList of Object into shared preferences from a Fragment. Below is what i tried

            SharedPreferences prefs = getActivity().getSharedPreferences("SHARED_PREFS_FILE", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            try {
               editor.putString("taggedFriends",ObjectSerializer.serialize(taggableFriends));

            } catch (IOException e) {
                e.printStackTrace();

Its showing error at ObjectSerializer

**Cannot resolve symbom 'ObjectSerializer'** 

I tried

getActivity.ObjectSerializer.serialize(..);

But error didn't go. Help me what can i do now.

Thankyou for spending time for me.

Community
  • 1
  • 1
Ganesh
  • 1,820
  • 2
  • 20
  • 40

2 Answers2

1

Try this:

Last edit:

In this case:

 static class YourObject
{
    private String _name;

    public YourObject(String name)
    {
        this._name = name;  
    } 
}


YourObject yourObject = new YourObject(myName);
ArrayList<YourObject> foo = new ArrayList<YourObject>();
foo.add(yourObject);

convert an ArrayList to JSONArray:

  JSONArray mJSONArray = new JSONArray(foo);

Then save the JSONArray:

 SharedPreferences.Editor editor = prefs.edit();
 editor.putString("yourStringName", mJSONArray.toString());

String to JSONArray:

SharedPreferences prefs = getSharedPreferences("SHARED_PREFS_FILE",   Context.MODE_PRIVATE);
String myJSONArrayString = prefs.getString("yourStringName", "");
JSONArray jsonArray = new JSONArray(myJSONArrayString);

JSONArray to ArrayList:

ArrayList<String> list = new ArrayList<String>();     
   for (int i=0;i<jsonArray.length();i++){ 
    list.add(jsonArray.get(i).toString());
   } 

I hope this solve your question.

Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32
  • You are using ArrayList but i am using ArrayList , will it cause any problem > – Ganesh Dec 07 '15 at 10:38
  • Thank you, I'll try it and then say you. :) Please wait for a minute – Ganesh Dec 07 '15 at 10:50
  • It would be useful if you say me how to retrive it in another activity. . – Ganesh Dec 07 '15 at 10:54
  • : Sorry , it may be my problem in my Class. I don't know how to solve it. I'll write another question and paste link here. Will you please wait for 10 minutes. I really need your help . Thanks.. – Ganesh Dec 07 '15 at 11:03
  • I think your code is correct, but i am having some other issues in my Class. I hope you can solve it . I'll paste my another questions link here in 5 minutes. Please help me.... – Ganesh Dec 07 '15 at 11:13
  • Ok no problem ill wait – Fabio Venturi Pastor Dec 07 '15 at 11:18
  • Sorry to make you wait... here is the link "http://stackoverflow.com/questions/34132418/why-cant-i-use-my-arraylist-variable-outside-facebookcallback" . – Ganesh Dec 07 '15 at 11:25
  • The Apoorva answer solve your question, put the code inside onSuccess . – Fabio Venturi Pastor Dec 07 '15 at 11:40
  • I just did, now its having a value. I followed your method but i got a problem . --> JSONArray mJSONArray = new JSONArray(foo); <-- After this, i checked with Log.d(TAG,mJSONArray.toString()); , what i got is null – Ganesh Dec 07 '15 at 11:48
  • Are you sure if taggableFriends is not null? – Fabio Venturi Pastor Dec 07 '15 at 11:59
  • Yes i am sure. @Fabio Venturi Pastor – Ganesh Dec 07 '15 at 12:16
  • You can see on this example how to convert Arraylist in a JsonArray: https://javanotes2all.wordpress.com/2013/03/05/convert-arraylist-to-jsonarray/ Im not sure what is your problem but now im at work, in 2 hrs ill help you. – Fabio Venturi Pastor Dec 07 '15 at 12:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97401/discussion-between-fabio-venturi-pastor-and-ganesh). – Fabio Venturi Pastor Dec 09 '15 at 11:18
1

First add Gson to your gradle:

compile 'com.google.code.gson:gson:2.2.4'

Convert your list to Json String like below:

List<String> foo = new ArrayList<String>();
foo.add("Item1");
foo.add("Item2");
foo.add("Item3");

String json = new Gson().toJson(foo );

And save it to shared pref like below:

SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putString("yourKey", json);

And when you want to use read your saved json string from pref:

String json = mPrefs.getString("yourKey", "");

Convert your Json String to list of your objects like below. In example i used String.

ArrayList<String> foo = (ArrayList<String>) new Gson().fromJson(json,
                    new TypeToken<ArrayList<String>>() {
                    }.getType());
savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • hey thanks for the answer. will it work for ArrayList . What you are saying is for ... Correct me if i am wrong – Ganesh Dec 07 '15 at 10:00
  • It'll work for your custom objects too. If you have problem we can discuss. – savepopulation Dec 07 '15 at 10:03
  • Also another doubt. I am having object of ArrayList . Can i use ArrayList obj .toJson(obj) or Do i need to use only List object ? – Ganesh Dec 07 '15 at 10:03
  • Its saying casting these is redundent while retreving, shall i remove casting in this line 'ArrayList foo = (ArrayList) new Gson().fromJson(json, new TypeToken>() { }.getType());' – Ganesh Dec 07 '15 at 10:20
  • its just storing "[ ]" value in json string. :-/ its not actually storing any string. – Ganesh Dec 07 '15 at 10:46