0

I have an ArrayList of String objects that is keeping values as needed.
now i want to keep it save either as a FILE or in SharedPreferences or converting it into JSON ARRAY and saving..
My array list is like this:

  ArrayList<Struct_Saved_Domains> arraylist = new ArrayList<Struct_Saved_Domains>();

How can i do this? Any Idea??

waqaslam
  • 67,549
  • 16
  • 165
  • 178
Noman
  • 4,049
  • 10
  • 38
  • 59
  • check this answer here http://stackoverflow.com/questions/12350800/android-how-to-store-array-of-strings-in-sharedpreferences-for-android?lq=1 – Marko Niciforovic Mar 01 '13 at 14:17

2 Answers2

2

You may use Gson to convert your object into string and then save it in shared preferences:

SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putStringSet("myList", new Gson().toJson(arraylist).toString());
editor.apply();
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • How to add the Gson?? Any jar file? – Noman Mar 04 '13 at 05:56
  • download the library's jar file and put it inside **libs** folder of your project. If you dont have such folder then create one manually. Afterwards you are ready to go. – waqaslam Mar 04 '13 at 08:06
  • @Waqas.. yeah i got it now.. its included..Thanks – Noman Mar 04 '13 at 09:12
  • @Waqas.. Shared pref is not reliable.. like when i exit the app and reload it and try to save new values then the old values are gone. CAn u suggest me any other way to keep old values in the array and adding new ones ?? – Noman Mar 06 '13 at 08:33
  • which android API version are you using for your app? if its gingerbread or above then please call `apply()` instead of `commit()` on your preference editor. – waqaslam Mar 06 '13 at 08:48
  • @Waqas.. i had put this question http://stackoverflow.com/questions/15242506/store-arraylist-of-cutom-objects/15242639?noredirect=1#15242639 Got some idea from it :) – Noman Mar 06 '13 at 08:51
  • ahh, you had to append the new values in the old one by reading it first. however, if you want to add new entries in the preference storage then simply introduce more new keys like myList1, myList2, myList2, etc. – waqaslam Mar 06 '13 at 09:06
1

You can use the below code for shared preferences.

public class WebService {

    String PREF_NAME = "PREF_NAME";
    public static SharedPreferences sp;
    Editor spEdit;

    public String UserID;
    public String Password;


    public LoginWebService(Context ctx) {
        // TODO Auto-geneated constructor stub
        sp = ctx.getSharedPreferences(PREF_NAME, 0);
        spEdit = sp.edit();
    }

    public void setUserID(String UserID) {
        spEdit.putString("UserID", UserID);
        spEdit.commit();

    }

    public String getUserID() {
        return sp.getString("UserID", "");    
    }

    public String getPassword() {
        return sp.getString("Password", "");
    }

    public void setPassword(String Password) {
        spEdit.putString("Password", Password);
        spEdit.commit();
    }





    public void clear(){
        spEdit.clear();
        spEdit.commit();
    }

}

While saving the data in the shared preferences you can use like below.

WebService objlogin=new WebService(context);
objlogin.clear();

for (int i = 0; i <arraylist.size(); i++) 
{
objlogin.setUserID(arraylist.get(i).something); 
objlogin.setPassword(arraylist.get(i).something); 

}

Then you all the data's will be saved in the shard preferences.

Hope this will help you.

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31