【问题标题】:How to put Arraylist of Custom Object in a SharedPreference如何将自定义对象的 Arraylist 放入 SharedPreference
【发布时间】:2015-12-23 10:13:38
【问题描述】:

我想在 sharedPreference 中保存自定义类型 Object 的 Arraylist。为此,我将 Arraylist 传递给 Gson,没有编译错误,但运行此代码设备后等待很长时间并卡住了。如果我走错了路,请给我其他建议。

public void storeApps(Context context, List<AppInfo> apps) {
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();
    Gson gson = new Gson();
    String jsonData = gson.toJson(apps);// this line wait for long time at compile time.
    editor.putString(Fav, jsonData);
    editor.commit();
}

AppInfo 类

public class AppInfo {
    public String appname = "";
    public String pname = "";
    public String versionName = "";
    public int versionCode = 0;
    public Drawable icon;
    public int color = 0;

    public String getAppname() {
        return appname;
    }

    public String getVersionName() {
        return versionName;
    }

    public String getPname() {
        return pname;
    }

    public Drawable getIcon() {
        return icon;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public int getColor() {
        return color;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public AppInfo() { }

    public AppInfo(String appname, String pname, String versionName, int versionCode, Drawable icon, int color) {
        this.appname = appname;
        this.pname = pname;
        this.versionName = versionName;
        this.versionCode = versionCode;
        this.icon = icon;
        this.color = color;
    }
}

【问题讨论】:

  • 实际上应用程序信息是预定义的类,这就是 Gson 无法转换它的原因。您需要创建自己的类,仅在您想要的字段中存储所需的数据。
  • @Rohit Heera AppInfo 是我创建的一个类。它不是预定义的类
  • 好的,列表的大小是多少
  • @RohitHeera apps.size() 是 29
  • 当我可以转换联系人列表时,我可以发布一个在我的情况下工作正常的代码..

标签: android arraylist sharedpreferences


【解决方案1】:
class Mydata 

{

ArrayList<AppInfo> data;

    public Mydata (List<AppInfo> data) {

        this.data= data;

    }

    public List<AppInfo> getContactSyn() {
        return data;
    }

    public void setContactSyn(List<AppInfo> data) {
        this.data= data;
    }


}

 Gson gson = new Gson();
MyData datalist = new MyData(data);

    String jsonData = gson.toJson(datalist);

【讨论】:

  • 其实你可以隐蔽列表转换单个对象而不是对象列表是个好习惯
  • 同样的问题再次出现
  • @njzk2 我觉得来源没问题。
  • @Tarikhelian 我在问 Rohit 是否有任何来源来支持这种说法[转换单个对象是“良好做法”]
  • 我也应用了 Rohit 源,但结果相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2014-01-17
  • 2017-01-26
  • 1970-01-01
  • 2016-01-05
  • 2015-06-17
相关资源
最近更新 更多