在Android中我们经常需要持久化保存数据,这时可以使用文件,SharePreference,以及SQLite等。如果我想保存一个对象呢?有些人会立马想到用orm框架,但是我可能不想依赖这个框架。实际上如果数据量不大,用SharePreference可能会更适合。我们完全可以自己实现一个SharePreference版的持久化保存对象的工具类。
直接看代码
public class SpUtils {
private static final String DEFAULT_SP_NAME = "default_sp";
public static <T> T getObject(Context context, Class<T> clazz) {
String key = getKey(clazz);
String json = getString(context, key, null);
if (TextUtils.isEmpty(json)) {
return null;
}
try {
Gson gson = new Gson();
return gson.fromJson(json, clazz);
} catch (Exception e) {
return null;
}
}
public static void putObject(Context context, Object object) {
String key = getKey(object.getClass());
Gson gson = new Gson();
String json = gson.toJson(object);
putString(context, key, json);
}
public static void removeObject(Context context, Class<?> clazz){
remove(context, getKey(clazz));
}
public static String getKey(Class<?> clazz) {
return clazz.getName();
}
public static void remove(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.remove(key);
edit.commit();
}
public static void putString(Context context, String key, String value) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public static String getString(Context context, String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
return sp.getString(key, defValue);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- putObject()方法中,我们以对象的类名字作为key,以对象的json字符串作为value保存到SharePreference中。
- getObject()方法,我们先获取类的名字,再将它作为key,然后从SharePreference中获取对应的字符串,然后通过Gson将json字符串转化为对象。
到目前为止,看起来还不错,但是聪明的你可能会发现,如果我要保存的类是一个泛型呢,比如List<Person>
,用上面的方法肯定时不行了,因为泛型会被擦除,List<String>
和List<Person>
都是同一个类。关于泛型擦除可参考:Java 的泛型擦除和运行时泛型信息获取。
那么对于泛型该如何处理呢?
回顾用Gson解析泛型类时,我们可能写过类似下面的代码:
String json = result;
Gson gson = new Gson();
Type type = new TypeToken<List<Person>>() {/*注意这里有个大括号*/}.getType());
List<Person> list = gson.fromJson(json, type);
- 1
- 2
- 3
- 4
这里用TypeToken去获取泛型的信息。
Ok,我们已经有思路了
public class SpUtils {
// 获取一个泛型的对象
public static <T> T getObject(Context context, Type type) {
String key = getKey(type);
String json = getString(context, key, null);
if (TextUtils.isEmpty(json)) {
return null;
}
try {
Gson gson = new Gson();
return gson.fromJson(json, type);
} catch (Exception e) {
return null;
}
}
// 保存一个泛型的对象
public static void putObject(Context context, Object object, Type type) {
String key = getKey(type);
Gson gson = new Gson();
String json = gson.toJson(object);
putString(context, key, json);
}
public static void removeObject(Context context, Type type) {
remove(context, getKey(type));
}
public static String getKey(Type type) {
return type.toString();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- getObject()方法中,使用Type参数代替原来的Class
- putObject()方法中,新增一个Type参数,用来获取SharePreference中的key。
使用也不难:
List<Person> list = new ArrayList<>();
Person p = new Person(20, "张三1");
list.add(p);
p = new Person(20, "张三2");
list.add(p);
// 保存一个泛型对象
SpUtils.putObject(context, list,
new TypeToken<List<Person>>() { }.getType());
// 获取一个泛型对象
List<Person> list2 = SpUtils.getObject(context,
new TypeToken<List<Person>>() { }.getType());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
完整代码如下:
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.google.gson.Gson;
import java.lang.reflect.Type;
public class SpUtils {
private static final String DEFAULT_SP_NAME = "default_sp";
// 通过类名字去获取一个对象
public static <T> T getObject(Context context, Class<T> clazz) {
String key = getKey(clazz);
String json = getString(context, key, null);
if (TextUtils.isEmpty(json)) {
return null;
}
try {
Gson gson = new Gson();
return gson.fromJson(json, clazz);
} catch (Exception e) {
return null;
}
}
// 通过Type去获取一个泛型对象
public static <T> T getObject(Context context, Type type) {
String key = getKey(type);
String json = getString(context, key, null);
if (TextUtils.isEmpty(json)) {
return null;
}
try {
Gson gson = new Gson();
return gson.fromJson(json, type);
} catch (Exception e) {
return null;
}
}
/**
* 保存一个对象,object必须是普通类,而不是泛型,如果是泛型,请使用 {@link SpUtils#putObject(Context, Object, Type)}
*
* @param context
* @param object
*/
public static void putObject(Context context, Object object) {
String key = getKey(object.getClass());
Gson gson = new Gson();
String json = gson.toJson(object);
putString(context, key, json);
}
/**
* 保存一个泛型对象
*
* @param context
* @param object
* @param type 如果你要保存 List<Person> 这个类, type应该 传入 new TypeToken<List<Person>>() {}.getType()
*/
public static void putObject(Context context, Object object, Type type) {
String key = getKey(type);
Gson gson = new Gson();
String json = gson.toJson(object);
putString(context, key, json);
}
public static void removeObject(Context context, Class<?> clazz) {
remove(context, getKey(clazz));
}
public static void removeObject(Context context, Type type) {
remove(context, getKey(type));
}
public static String getKey(Class<?> clazz) {
return clazz.getName();
}
public static String getKey(Type type) {
return type.toString();
}
public static void remove(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.remove(key);
edit.commit();
}
public static void putString(Context context, String key, String value) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public static String getString(Context context, String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
return sp.getString(key, defValue);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104