2016-07-25 28 views
0

在我的應用程序中,我想存儲應用程序圖標列表ArrayList。這裏是我的代碼從哪裏獲得的應用程序圖標圖像:哪種類型的ArrayList用於存儲android中的可繪製變量

for(String a:array){ //array contains list of appplications package name 
    try{ 
     Drawable icon=getPackageManager().getApplicationIcon(a); 
    } 
    catch (PackageManager.NameNotFoundException e){ 
     Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show(); 
    } 
} 

在這裏,我想存儲所有從icon變量進入一個ArrayList應用程序圖標。但是,如何在ArrayList中存儲所有應用程序圖標圖像,因爲我的圖標是在運行時生成的。 那麼,如何將所有Drawable icons添加到ArrayList中?

回答

0

您可以直接創建自己的ArrayList<Drawable>並填充如下:

final List<Drawable> icons = new ArrayList<Drawable>(); 
final PackageManager mgr = getPackageManager(); 
for (final String app : array) { 
    try { 
     final Drawable icon = mgr.getApplicationIcon(app); 
     icons.add(icon); 
    } catch (final PackageManager.NameNotFoundException ex) { 
     Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); 
    } 
} 
/* now we have our icon Drawables in a list */ 
+0

確定您的代碼是正確的。現在我可以將圖標存儲在Drawable ArrayList中。還有一個問題,我怎樣才能使用ListView的Drawable Arraylist。因此,我可以通過ListView顯示圖標 – Rachan

0

嘗試下面的代碼......這樣一來,你可以存儲/通過一鍵檢索繪製(這是包名)

HashMap<String,Drawable> drawableList = new HashMap<String,Drawable>(); 
for(String a:array){ //array contains list of appplications package name 
    try{ 
     Drawable icon=getPackageManager().getApplicationIcon(a); 
     drawableList.put(a, icon); 
    } 
    catch (PackageManager.NameNotFoundException e){ 
     Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show(); 
    } 
} 

或者,你可以使用ArraList和存儲僅繪項目:

ArrayList<Drawable> drawableList = new ArrayList<Drawable>(); 
for(String a:array){ //array contains list of appplications package name 
    try{ 
     Drawable icon=getPackageManager().getApplicationIcon(a); 
     drawableList.add(icon); 
    } 
    catch (PackageManager.NameNotFoundException e){ 
     Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

由於其WO rking :) – Rachan

相關問題