Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Strings from ArrayList and save in Shared Preferences

I have an ArrayList that can contain 1 to 15 strings. Those strings need to be saved in my shared preferences, now I know how to iterate trough the array to get the strings. What I don't know, is there a clean way to dynamically add the strings to my shared preferences file? I could do this on a sluggish way by creating 15 strings and using an if statement to fill the shared preference, but I would like to know if there is a better way.

Thank you.

like image 386
Raymond P Avatar asked Sep 27 '26 00:09

Raymond P


1 Answers

If its about naming, you can use something like this:

public static final String mPrefix = "mArray";
SharedPreferences prefs;
prefs  = this.getSharedPreferences("PREF", 0);
prefsEditor = appSharedPrefs.edit();

//mAL is your ArrayList

for(int i=0; i<mAl.size(); i++){

prefsEditor.putString(mPrefix + "-" + i, mAl.get(i));
}
prefsEditor.commit();
like image 132
Archie.bpgc Avatar answered Sep 28 '26 13:09

Archie.bpgc