23

This method add new object into ArrayList

//get text from textview
time = date.getText().toString();
entry_d = entry.getText().toString();
dayName = day.getText().toString();

arrayList.add( new ArrayObject( dayName, entry_d ,time));

I am trying to add these 3 strings in SharedPrefrences. Here is my code:

private void savePreferences(String key, String value) {

    SharedPreferences sharedPreferences = PreferenceManager             
                                     .getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

This method only add one string at a time where as I want to add 3 strings in one go. Is there any method I can implement.

1

3 Answers 3

87

Convert your array or object to Json with Gson library and store your data as String in json format.

Save;

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
Gson gson = new Gson();

String json = gson.toJson(arrayList);

editor.putString(TAG, json);
editor.commit();

Read;

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString(TAG, "");
Type type = new TypeToken<List<ArrayObject>>() {}.getType();
List<ArrayObject> arrayList = gson.fromJson(json, type);
Sign up to request clarification or add additional context in comments.

5 Comments

For which Type Classes to import - stackoverflow.com/questions/20847997/…
use import java.lang.reflect.Type;
what to do if arrayList is List<View> ?
Do not persist framework object or any complex object like view.
Two days I tried to add json content to array list. Thank you. I used get type and everything works
22

Store Arraylist Using Shared Preferences

SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
Editor edit=prefs.edit();

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist Name);
edit.putStringSet("yourKey", set);
edit.commit();

Retrieve Arraylist from Shared Preferences

Set<String> set = prefs.getStringSet("yourKey", null);
List<String> sample=new ArrayList<String>(set);

6 Comments

can we use a single set do add more than 1 array list...I have 3 string array list.May I use single set or 3 sets for 3 lists
@Prabs did you create separate sets/?
@AnshulTyagi this sets concepts didn't worked for my requirement..So I didn't implemented this concept..For testing,Yes I've created separate sets.
Sorry for that. it will support only Set<String> not others.
it's only for Set<String> :\ !
|
3

Don't use Hashset for this. It will change the ordering of Arraylist. Use Gson instead. If you wish to use Hashset, you will have to serialize and deserialize which will take up resources.

Comments

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.