使用SharedPreferences在Android存储对象详细代码
程序员文章站
2022-07-02 08:34:56
为什么不用sqlite? 原因多种:除了面向对象和关系数据库之间的阻抗不匹配时,sqlite可能是矫枉过正(带来了更多的开销)对于一些简单的用例,或者其用法和语句可能会完全不喜欢。其他andr...
为什么不用sqlite
? 原因多种:除了面向对象和关系数据库之间的阻抗不匹配时,sqlite可能是矫枉过正(带来了更多的开销)对于一些简单的用例,或者其用法和语句可能会完全不喜欢。其他android orm
框架(ormlite, greendao, sugar orm, activeandroid, 等)或nosql
移动数据库,如couchbase
精简版(beta版在此时间)。 couchbase精简版基本上是一个json数据库,它们旨在降低复杂性,但是同时又违背了抽象漏洞定律(所有不证自明的抽象都是有漏洞的)
代码如下:
/** stores the user object in sharedpreferences */ public class userprefs{ /** this application's preferences label */ private static final string prefs_name = "com.our.package.userprefs"; /** this application's preferences */ private static sharedpreferences settings; /** this application's settings editor*/ private static sharedpreferences.editor editor; /** constructor takes an android.content.context argument*/ public userprefs(context ctx){ if(settings == null){ settings = ctx.getsharedpreferences(prefs_name, context.mode_private ); } /* * get a sharedpreferences editor instance. * sharedpreferences ensures that updates are atomic * and non-concurrent */ editor = settings.edit(); } //... }
其中user代码如下:
/** user object to be saved in db */ public class user{ private int id; // used for object storage private string username; private boolean registered; private double score; /** constructor */ 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操作如下:
/** generic field keys */ 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"; /** store or update */ 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(); } /** retrieve */ 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); } /** delete */ 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()
给我们每个用户每个字段的唯一标识。
/** the prefix for flattened user keys */ public static final string key_prefix = "com.our.package.key"; /** method to return a unique key for any field belonging to a given object * @param id of the object * @param fieldkey of a particular field belonging to that object * @return key string uniquely identifying the object's field */ 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存储对象内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!