为什么不用SQLite
? 原因多种:除了面向对象和关系数据库之间的阻抗不匹配时,SQLite可能是矫枉过正(带来了更多的开销)对于一些简单的用例,或者其用法和语句可能会完全不喜欢。其他Android ORM
框架(ORMLite, greenDAO, Sugar ORM, ActiveAndroid, 等)或NOSQL
移动数据库,如Couchbase
精简版(Beta版在此时间)。 Couchbase精简版基本上是一个JSON数据库,它们旨在降低复杂性,但是同时又违背了抽象漏洞定律(所有不证自明的抽象都是有漏洞的)
代码如下:
public class UserPrefs{
private static final String PREFS_NAME = "com.our.package.UserPrefs";
private static SharedPreferences settings;
private static SharedPreferences.Editor editor;
public UserPrefs(Context ctx){
if(settings == null){
settings = ctx.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE );
}
editor = settings.edit();
}
//...
}
其中User代码如下:
public class User{
private int id; // used for object storage
private String userName;
private boolean registered;
private double score;
public User(int id, String userName, boolean registered, double score){
this.id = id;
this.userName = userName;
this.registered = registered;
this.score = score;
}
// getters and setters here...
}
将SharedPreferences
看成一个Map存储,
CRUD操作如下:
private static final String KEY_USERNAME = "com.our.package.KEY_USERNAME";
private static final String KEY_REGISTERED = "com.our.package.KEY_REGISTERED";
private static final String KEY_SCORE = "com.our.package.KEY_SCORE";
public void setUser(User user){
if(user == null)
return; // don't bother
int id = user.getId();
editor.putString(
getFieldKey(id, KEY_USERNAME),
user.getUsername() );
editor.putBoolean(
getFieldKey(id, KEY_REGISTERED),
user.isRegistered() );
editor.putFloat(
getFieldKey(id, KEY_SCORE),
user.getScore() );
editor.commit();
}
public User getUser(int id){
String name = settings.getString(
getFieldKey(id, KEY_USERNAME),
"" ); // default value
boolean registered = settings.getBoolean(
getFieldKey(id, KEY_REGISTERED),
false); // default value
double score = settings.getFloat(
getFieldKey(id, KEY_SCORE),
0); // default value
return new User(id, name, registered, score);
}
public void deleteUser(User user){
if(user == null)
return; // don't bother
int id = user.getId();
editor.remove( getFieldKey(id, KEY_USERNAME) );
editor.remove( getFieldKey(id, KEY_REGISTERED) );
editor.remove( getFieldKey(id, KEY_SCORE) );
editor.commit();
}
主键是通过getFieldKey
方法, getFieldKey()
给我们每个用户每个字段的唯一标识。
public static final String KEY_PREFIX =
"com.our.package.KEY";
private String getFieldKey(int id, String fieldKey) {
return KEY_PREFIX + id + "_" + fieldKey;
}
客户端调用如下:
// get a SharedPreferences instance
UserPrefs prefs = new UserPrefs( this.getApplicationContext() );
// get id from server or local storage
// then find User with that id
User user = prefs.getUser(id);
// operations on User, e.g.
user.setRegistered(true);
user.setScore(new_score);
// save
prefs.setUser(user);
// ...or delete
prefs.deleteUser(user),
Gson
Gson
是一个Java
库,它提供了简单的toJSON()
和fromJson()
方法来转换Java
对象到JSON
格式,反之亦然。我们可以简单地存储JSON
格式整个字符串到SharedPreferences
:
// convert User object user to JSON format
Gson gson = new Gson();
String user_json = gson.toJson(user);
// store in SharedPreferences
String id = "" + user.getId(); // get storage key
editor.putString(id, user_json);
editor.commit();
// time flies...
// do the reverse operation
user_json = settings.getString(id, "");
user = gson.fromJson(user_json, User.class);
到此这篇关于使用SharedPreferences
在Android存储对象详细代码的文章就介绍到这了,更多相关使用SharedPreferences
在Android存储对象内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!