public class ICUResourceSave {
private static final String name = "icuResource";
private static SharedPreferences sp = null;
public ICUResourceSave() {
}
public static ICUResourceSave getInstance(Context context) {
sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
return new ICUResourceSave();
}
/**
* 先删除相同的key,然后保存
*
* @param map
*/
public void save(Map<String, Object> map) {
SharedPreferences.Editor editor = sp.edit();
Set<String> set = map.keySet();
for (String s : set) {
editor.remove(s);
editor.putString(s, (String) map.get(s));
}
editor.commit();
}
/**
* 先删除相同的key,然后保存
*
* @param map
*/
public void saveStatus(Map<String, Object> map) {
SharedPreferences.Editor editor = sp.edit();
Set<String> set = map.keySet();
for (String s : set) {
editor.remove(s);
editor.putInt(s, (Integer) map.get(s));
}
editor.commit();
}
/**
*
* @param map
*/
public void saveInt(Map<String, Object> map) {
SharedPreferences.Editor editor = sp.edit();
Set<String> set = map.keySet();
for (String s : set) {
editor.remove(s);
editor.putInt(s, (Integer) map.get(s));
}
editor.commit();
}
/**
* 把list数组放入sharedPreferences
*
* @param key
* @param list
*/
public void save(String key, List<Map<String, Object>> list) {
JSONArray jsonArray = new JSONArray();
for (Map<String, Object> map : list) {
Set<String> set = map.keySet();
JSONObject jsonObject = new JSONObject();
for (String s : set) {
try {
jsonObject.put(s, map.get(s));
} catch (JSONException e) {
e.printStackTrace();
}
}
jsonArray.put(jsonObject);
}
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, jsonArray.toString());
editor.commit();
}
/**
* 根据key获取某个值
*
* @param key
* @return
*/
public Object get(String key) {
return sp.getString(key, null);
}
/**
* 根据key获取某个值
*
* @param key
* @return
*/
public int getInt(String key) {
return sp.getInt(key, 0);
}
/**
* 获得array数组
*
* @param key
* @return
*/
public List<Map<String, Object>> getArray(String key) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String result = sp.getString(key, null);
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
JSONArray names = jsonObject.names();
if (names != null) {
for (int j = 0; j < names.length(); j++) {
String name = names.getString(j);
String value = jsonObject.getString(name);
map.put(name, value);
}
}
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
/**
* 获取所有的map
*
* @return
*/
public Map<String, ?> getAll() {
return sp.getAll();
}
/**
* 清除所有
*/
public void clearAll() {
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
}
说明:
1.context的模式
Context.MODE_PRIVATE 说明创建的这个文件是私有的只能被自身应用使用,而新文件会覆盖掉原有的文件,如果不想覆盖掉请采用Context.MODE_APPEND
另外除了刚才提到的两个,还有Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE,具体的用发请google一下。
2.sharePerferences的具体用法
sharePerferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
之后就可以调用editor的get()方法和put()方法,put()方法后用使用commit()提交数据,这点和数据库类似。
3.对于array数组的存储
对array数组的存储是一个稍微有一点麻烦的问题,我们可以使用json,把数组转成字符串,然后在递交,就可以,具体的使用方法请看我写的getArray(V) 方法