.Map nameIcons = new HashMap();
nameIcons.put("Noel", "/location/to/noel/icon.png");
nameIcons.put("Bob", "another/location/to/bob/icon.png");
nameIcons.put("another name", "last/location/icon.png");
SharedPreferences keyValues = getContext().getSharedPreferences("name_icons_list", Context.MODE_PRIVATE);
SharedPreferences.Editor keyValuesEditor = keyValues.edit();
for (String s : nameIcons.keySet()) {
// use the name as the key, and the icon as the value
keyValuesEditor.putString(s, nameIcons.get(s));
}
keyValuesEditor.commit()
New
public static void setPrefs(String key, String value, Context context){
SharedPreferences sharedpreferences = context.getSharedPreferences(Consts.SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(key, value);
editor.apply();
}public static String getPrefs(String key, Context context){
SharedPreferences sharedpreferences = context.getSharedPreferences(Consts.SP_NAME, Context.MODE_PRIVATE);
return sharedpreferences.getString(key, "notfound");
}
public static void setArrayPrefs(String arrayName, ArrayList array, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename"_,_0);__SharedPreferences.Editor_editor_=_prefs.edit();__editor.putInt(arrayName_+"_size"'>"preferencename", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(arrayName +"_size", array.size());
for(int i=0;i editor.putString(arrayName + "_" + i, array.get(i));
editor.apply();
}public static ArrayList getArrayPrefs(String arrayName, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
int size = prefs.getInt(arrayName + "_size", 0);
ArrayList array = new ArrayList<>(size);
for(int i=0;i array.add(prefs.getString(arrayName + "_" + i, null));
return array;
}
String value;Preferences.setPrefs("value","info",MainActivity.this);
value = Preferences.getPrefs("value",SomeActivity.this)
ArrayList mOrderList = new ArrayList();Preferences.setArrayPrefs("OrderList",mOrderList,context);
mOrderList= Preferences.getArrayPrefs("OrderList",context);
// my list of names, icon locations
Map nameIcons = new HashMap();
nameIcons.put("Noel", "/location/to/noel/icon.png");
nameIcons.put("Bob", "another/location/to/bob/icon.png");
nameIcons.put("another name", "last/location/icon.png");
SharedPreferences keyValues = getContext().getSharedPreferences("name_icons_list", Context.MODE_PRIVATE);
SharedPreferences.Editor keyValuesEditor = keyValues.edit();
for (String s : nameIcons.keySet()) {
// use the name as the key, and the icon as the value
keyValuesEditor.putString(s, nameIcons.get(s));
}
keyValuesEditor.commit()
public boolean saveArray(String[] array, String arrayName, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(arrayName +"_size", array.length);
for(int i=0;i editor.putString(arrayName + "_" + i, array[i]);
return editor.commit();
}
public String[] loadArray(String arrayName, Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
int size = prefs.getInt(arrayName + "_size", 0);
String array[] = new String[size];
for(int i=0;i array[i] = prefs.getString(arrayName + "_" + i, null);
return array;
}
Do'stlaringiz bilan baham: |