【发布时间】:2016-04-01 14:13:06
【问题描述】:
我想将 Drawable 转换为 int ,然后反之亦然。基本上我想将 Arraylist 对象保存到 sharedPrefrence强>。为此,我实施了 Gson 到 Srtring 的转换方法。如果我在这里使用 Drawable 而不是 int 那么 Gson 字符串转换需要很多时间。所以我想用 int 代替 Drawable。
private List<AppInfo> apps = null;
public void setIcon(int icon) {
this.icon = icon;
}
apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));
AppInfo 在这里
public AppInfo(String appname, String pname, String versionName, int versionCode, int icon, int color) {
this.appname = appname;
this.pname = pname;
this.versionName = versionName;
this.versionCode = versionCode;
this.icon = icon;
this.color = color;
}
这是将自定义对象的 ArrayList 转换为字符串的来源,以便我可以将其保存到 SharedPrefrence。
Gson gson = new Gson();
apps.get(number).setColor(picker.getColor());
String JsonAppsdata = gson.toJson(apps);
System.out.println("Storing="+JsonAppsdata);
utility.StoreData(getApplicationContext(), JsonAppsdata);
【问题讨论】:
-
您的资源中有所有图标吗?还是从服务器下载?
-
你如何填充 List
应用程序? -
第一次我从 res/drawable 填充它,然后如果有人(用户)想用系统应用程序图标中的某个人更改该图标,那么我需要获取系统安装应用程序图标。简而言之,有时我需要从 res/drawable 中获取,有时我需要通过此 mContext.getPackageManager().getApplicationIcon(apps.get(position).getPname()) 从系统中安装应用程序的图标中获取图标
-
那么是什么阻止您将此 mContext.getPackageManager().getApplicationIcon() int 保存到您的 AppInfo 中?
标签: android drawable android-sharedpreferences