2015-12-27 9 views
5

Ich möchte Drawable in int konvertieren und dann möchte ich vice versa.BasicallyArraylist Objekt in sharedPrefrence speichern. Zu diesem Zweck habe ich Implementieren Gson zu Sverring Konvertierungsmethode. Wenn ich hier Drawable anstelle von int dann verwenden Gson String Konvertierung viel Zeit. Daher möchte ich Int anstelle von Drawable verwenden.Wie Drawable in int und umgekehrt in Android konvertieren

private List<AppInfo> apps = null; 

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

    apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon())); 

Wo AppInfo hier ist

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; 
} 

ist hier Quelle Arraylist Custom Objekt in String Converting, so dass ich es zu SharedPrefrence speichern.

  Gson gson = new Gson(); 
      apps.get(number).setColor(picker.getColor()); 
      String JsonAppsdata = gson.toJson(apps); 
      System.out.println("Storing="+JsonAppsdata); 
      utility.StoreData(getApplicationContext(), JsonAppsdata); 
+0

Haben Sie alle Symbole in Ihren Ressourcen? Oder lädst du sie vom Server herunter? –

+0

Und wie bevölkern Sie die Liste Apps? –

+0

Zum ersten Mal bevölke ich es aus der res/Zeichnungs, dann nach diesem, wenn jemand (Benutzer) das Symbol mit etwas im System App-Symbol ändern will, dann muss ich System-Installation-Anwendungssymbol erhalten. Kurz gesagt, einige Zeit muss ich von res/drawable bekommen, und manchmal muss ich Icons von der Installation des Anwendungssymbols aus dem System bekommen, durch diese mContext.getPackageManager(). GetApplicationIcon (apps.get (position) .getPname()) –

Antwort

3

Int -> Drawable:

Drawable icon = getResources().getDrawable(42, getTheme());

Drawable -> Int:

(Ich gehe davon aus, dass Sie List<AppInfo> apps mit der App, deren Icons sind bevölkern bereits in res/drawable Ordner Ihrer App)

Sobald Sie setzen Ihre R.drawable.app1 zu ImageView, man kann es auch eine tag geben die Ressource in der ImageView später zu identifizieren:

ImageView appIcon1ImageView = (ImageView)findViewById(R.id.app_icon_1); 
    appIcon1ImageView.setImageDrawable(getDrawable(R.drawable.app1)); 
    appIcon1ImageView.setTag(R.drawable.app1); 

    ...... 
    // Once you need to identify which resource is in ImageView 
    int drawableId = Integer.parseInt(appIcon1ImageView.getTag().toString()); 

Wenn Ihre Symbole vom Server kommen werden - der einzige Weg ist to store them to disk und dann re- lade sie. (Oder, besser, sich auf den bereits vorhandenen Bild-Caching-Lösungen wie picasso)

UPD: Es gibt keinen direkten Weg Drawable in int umzuwandeln, aber in diesem speziellen Fall ist es möglich, die int zu bekommen, statt von Drawable von PackageManager:

ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1); 
int icon= applicationInfo.icon; 
+0

Es gibt ein Problem, dass ich konfrontiert ist, ist Resource ID nicht gefunden Ausnahme während ApplicationInfo applicationInfo = mContext.getPackageManager(). getApplicationInfo (apps.get (position) .getPname(), 1); int icon = Anwendungsinfo.icon; apps.get (position) .setIcon (applicationInfo.icon); // beim Zurückgehen int-> Drawable 'Drawable icon = getResources(). getDrawable (apps.get (i). getIcon()); ImageView imageView = (Bildansicht) rootView.findViewById (imageArray [i]); imageView.setImageDrawable (Symbol); ' –

0

Dies ist, wie wir App-Symbol und es für einen Imageview set holen kann.

    applicationInfo=mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),PackageManager.GET_META_DATA); 
        int icon= applicationInfo.icon; 
        Rsources resources=mContext.getPackageManager().getResourcesForApplication(applicationInfo); 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         holder.itemIcon.setImageDrawable(resources.getDrawable(icon,null)); 
        }else 
        { 
         holder.itemIcon.setImageDrawable(resources.getDrawable(icon)); 

        }