//Retrieve the values
Set<String> set = myScores.getStringSet("key", null);
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
// shared preferences
private SharedPreferences preferences;
private SharedPreferences.Editor nsuserdefaults;
// setup persistent data
preferences = this.getSharedPreferences("MyPreferences", MainActivity.MODE_PRIVATE);
nsuserdefaults = preferences.edit();
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
//Retrieve followers from sharedPreferences
Set<String> set = preferences.getStringSet("following", null);
if (set == null) {
// lazy instantiate array
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
} else {
// there is data from previous run
arrayOfMemberUrlsUserIsFollowing = new ArrayList<>(set);
}
// convert arraylist to set, and save arrayOfMemberUrlsUserIsFollowing to nsuserdefaults
Set<String> set = new HashSet<String>();
set.addAll(arrayOfMemberUrlsUserIsFollowing);
nsuserdefaults.putStringSet("following", set);
nsuserdefaults.commit();
public class MyApp extends Application {
//Pardon me for using global ;)
private ArrayList<CustomObject> globalArray;
public void setGlobalArrayOfCustomObjects(ArrayList<CustomObject> newArray){
globalArray = newArray;
}
public ArrayList<CustomObject> getGlobalArrayOfCustomObjects(){
return globalArray;
}
}
public static void setSharedPreferenceStringList(Context pContext, String pKey, List<String> pData) {
SharedPreferences.Editor editor = pContext.getSharedPreferences(Constants.APP_PREFS, Activity.MODE_PRIVATE).edit();
editor.putInt(pKey + "size", pData.size());
editor.commit();
for (int i = 0; i < pData.size(); i++) {
SharedPreferences.Editor editor1 = pContext.getSharedPreferences(Constants.APP_PREFS, Activity.MODE_PRIVATE).edit();
editor1.putString(pKey + i, (pData.get(i)));
editor1.commit();
}
} < / p >
和从共享优先级>>获取字符串列表
public static List<String> getSharedPreferenceStringList(Context pContext, String pKey) {
int size = pContext.getSharedPreferences(Constants.APP_PREFS, Activity.MODE_PRIVATE).getInt(pKey + "size", 0);
List<String> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(pContext.getSharedPreferences(Constants.APP_PREFS, Activity.MODE_PRIVATE).getString(pKey + i, ""));
}
return list;
}
Here Constants.APP_PREFS is the name of the file to open; can not contain path separators.
public class SharedPrefApi {
private SharedPreferences sharedPreferences;
private Gson gson;
public SharedPrefApi(Context context, Gson gson) {
this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
this.gson = gson;
}
...
public <T> void putList(String key, List<T> list) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, gson.toJson(list));
editor.apply();
}
public <T> List<T> getList(String key, Class<T> clazz) {
Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();
return gson.fromJson(getString(key, null), typeOfT);
}
}
使用
// for save
sharedPrefApi.putList(SharedPrefApi.Key.USER_LIST, userList);
// for retrieve
List<User> userList = sharedPrefApi.getList(SharedPrefApi.Key.USER_LIST, User.class);
Gson gson = new Gson();
// Save the size of the array
sharedPreferencesEditor.putInt("ArraySize", myArray.size());
for (int i=0; i<myArray.size(); i++) {
String key = "Array"+i;
String json = gson.toJson(myArray.get(i));
sharedPreferencesEditor.putString(key, json);
}
sharedPreferencesEditor.commit();
反序列化
// Get the size of the array to be deserialized. In my case, the default number should be 3
int arraySize = sharedPreferences.getInt("ArraySize",3);
myArray = new ArrayList<List<String>>();
for (int i=0; i<arraySize; i++) {
String key = "Array"+i;
String json = sharedPreferences.getString(key, null);
List<String> arrayTemp= gson.fromJson(json, List.class);
myArray.add(arrayTemp);
}
// My array may also include components with empty strings.
// Gson makes them null values and it is not possible
// to deserialize them as empty strings.
// The following takes care of that:
for (int i=0; i<myArray.size();i++) {
if (myArray.get(i) == null) {
List<String> emptyComponent = new ArrayList<String>() {
{ add(""); }
};
myArray.set(i,emptyComponent);
}
}