如何Android的共享preferences保存/存储对象对象、Android、preferences

由网友(怂。)分享简介:我的问题是登录我想保存/存储用户对象进行和行政Object.How我们可以实现这样的情景后?因为我需要的用户对象everywhere.This用户包含很多fields.I不能放在My question is after login i want to save/store user obejct and execut...

我的问题是登录我想保存/存储用户对象进行和行政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。 您可以立即下载这个罐子从这里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);
阅读全文

相关推荐

最新文章