封装一个工具类,自动拆分Object的属性,保存到SharedPreference。
场景是这样的:假如我们有很多个属性要保存到sharedprence中去,如果一个个的去commit会比较麻烦,而且不便于管理,所以我想将这些属性放在一个对象中,这个工具类就是用反射的方式遍历这个对象的属性,key就是属性名,value就椒属性值,自动将所有的属性和值保存到sharedprenced中去。
工具类如下:
import android.util.Log;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ClassSetAndGetKeyAndValue {
private OnGetCallBack mGetCall;
private OnSetCallBack mSetCall;
private Class<?> mClass;
private Object mObj;
private ClassSetAndGetKeyAndValue(Class<?> cs) {
mClass = cs;
}
public ClassSetAndGetKeyAndValue(Object obj, OnGetCallBack call) {
mObj = obj;
mGetCall = call;
}
public ClassSetAndGetKeyAndValue(Class<?> cs, OnSetCallBack call) {
this(cs);
mSetCall = call;
}
/**
* 获取集合
*
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public List<Object> startRunDatas() throws InstantiationException, IllegalAccessException {
boolean is = true;
List<Object> lists = new ArrayList<Object>();
while (is) {
mObj = mClass.newInstance();
Object obj = startRunData();
if (obj == null) {
return lists;
}
Log.i("you" , "添加了一个对象"+obj.toString());
lists.add(obj);
Log.i("you" , "添加对象成功个数"+lists.size());
}
return lists;
}
/**
* 加载数据
*
* @throws IllegalAccessException
* @throws InstantiationException
*/
public Object startRunData() throws InstantiationException, IllegalAccessException {
boolean is = true;
if (mObj == null)
mObj = mClass.newInstance();
Class<? extends Object> c = mObj.getClass();
for (; c != Object.class; c = c.getSuperclass()) {
Field fs[] = c.getDeclaredFields();
for (Field f : fs) {
String name = f.getName();
String value = "";
f.setAccessible(true);
if (mSetCall != null) {
Object obj = mSetCall.setCall(name, is);
is = false;
if (obj == null) return null;
if (obj instanceof String) {
String v = obj.toString();
if (v.length() != 0)
f.set(mObj, v);
continue;
}else{
f.set(mObj, obj);
}
} else {
if (f.get(mObj) instanceof String) {
value = (String) f.get(mObj);
}
}
if (mGetCall != null) {
mGetCall.onCall(name, value);
}
}
}
return mObj;
}
public static void getObjectListKeyAndValue(Object obj, final List<String> keys, final List<String> values) throws InstantiationException, IllegalAccessException {
new ClassSetAndGetKeyAndValue(obj, new OnGetCallBack() {
@Override
public void onCall(String key, String value) {
if (keys != null)
keys.add(key);
if (values != null)
values.add(value);
}
}).startRunData();
}
public interface OnSetCallBack {
/**
* @param key 需要获取值的key
* @param isStart 是否是重新开始的
* @return
*/
public Object setCall(String key, boolean isStart);
}
public interface OnGetCallBack {
public void onCall(String key, String value);
}
}
public class DataSharedPreferences {
private Context mCon;
public DataSharedPreferences(Context context){
mCon = context;
}
public Object getData(Class<?> cs , String name ) throws InstantiationException, IllegalAccessException{
return getData(cs , name , Context.MODE_PRIVATE);
}
public Object getData(Class<?> cs , String name , int mode) throws InstantiationException, IllegalAccessException{
final SharedPreferences share = getSharedPreferences(name, mode);
share.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
}
});
ClassSetAndGetKeyAndValue kv = new ClassSetAndGetKeyAndValue(cs, new ClassSetAndGetKeyAndValue.OnSetCallBack() {
@Override
public String setCall(String key , boolean isStart) {
return share.getString(key, "");
}
});
return kv.startRunData();
}
public boolean saveData(Object obj ,String name , int mode){
final SharedPreferences share = getSharedPreferences(name, mode);
final SharedPreferences.Editor editor = share.edit();
try {
new ClassSetAndGetKeyAndValue(obj, new ClassSetAndGetKeyAndValue.OnGetCallBack() {
@Override
public void onCall(String key, String value) {
editor.putString(key, value);
}
}).startRunData();
} catch (InstantiationException e) {
e.printStackTrace();
return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
return false;
}
editor.commit();
return true;
}
public void clearData(String name , int mode){
final SharedPreferences share = getSharedPreferences(name, mode);
share.edit().clear().commit();
}
private SharedPreferences getSharedPreferences(String name , int mode){
return mCon.getSharedPreferences(name, mode);
}
}
使用方法:
//取对象
public Object getDataFromSharePreferences(Class<?> cs, String fileName) {
mSharePreferences = getDataSharePreferences();
try {
return mSharePreferences.getData(cs, fileName);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
//存对象
public boolean saveDataToSharePreferences(Object obj, String fileName) {
mSharePreferences = getDataSharePreferences();
return mSharePreferences.saveData(obj, fileName, Activity.MODE_PRIVATE);
}