如何Android的共享preferences保存/存储对象 [英] How Android SharedPreferences save/store object
本文介绍了如何Android的共享preferences保存/存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是登录我想保存/存储用户对象进行和行政Object.How我们可以实现这样的情景后?因为我需要的用户对象everywhere.This用户包含很多fields.I不能放在
My question is after login i want to save/store user obejct and executive Object.How we can implement this kind of scenario? because of I need to get the user object everywhere.This user contain many fields.I can't put in the
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("BusinessUnit", strBusinessUnit);
这样的。
请帮我
在此先感谢
推荐答案
您可以使用gson.jar存储类对象为共享preferences。 您可以立即下载这个罐子从这里<一个href="https://$c$c.google.com/p/google-gson/downloads/list">https://$c$c.google.com/p/google-gson/downloads/list
You can use gson.jar to store class objects into SharedPreferences. You can downlaod this jar from here https://code.google.com/p/google-gson/downloads/list
//Creating a shared preference
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
要保存
Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
中检索
Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);
这篇关于如何Android的共享preferences保存/存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文