2012-08-05 132 views
8

我正在開發一個Android項目,並且我有很多drawables。這些可繪製的圖形都被命名爲icon_0.png,icon_1.png ... icon_100.png。我想將這些可繪製的所有資源ID添加到整數的ArrayList。 (對於那些不懂android的人,只有Java,我在談論靜態變量,在一個類的靜態內部類中,如R.drawable.icon_0。所有這些靜態變量都是整型。)按循環名稱訪問變量

是否有更高效如何做到這一點,而不是逐個添加它們?像

ArrayList<Integer> list = new ArrayList<Integer>(); 
list.add(R.drawable.icon_1); 
list.add(R.drawable.icon_2); 
... 
list.add(R.drawable.icon_100); 

我可以通過它們以某種方式循環?像

for(int i=0; i<100; i++) 
{ 
    list.add(R.drawable.icon_+i); //<--- I know this doesn't work. 
} 

我沒有控制這些靜態整數的文件,我不能在運行時創建繪圖。

任何幫助,將不勝感激!

編輯

好吧,我讀了答案,但我有一個大問題:我沒有訪問任何Context情況下,我需要創建ID的數組/列表(我做在靜態initialzer塊),所以getResources()方法,建議的兩個答案不會工作。有沒有其他的方式來做到這一點?

+0

http://stackoverflow.com/questions/2414134/dynamically-get-drawables-by-id – 2012-08-05 06:48:26

回答

0

一種方法是使用反射API。

沿東西線...

Field[] fields = R.drawable.class.getFields(); 
List<String> names = new ArrayList<String>(); 
for (Field field : fields) { 
    if(field.getName().startsWith("icon")) 
     names.add(field.getName());  
} 

int resid = getResources().getIdentifier(names.get(0), "drawable", "com.org.bla"); 

我沒有測試過這一點,但你的想法。

+1

壞主意......國際海事組織。 – 2012-08-05 07:21:03

+0

我想知道爲什麼... – nullpotent 2012-08-05 07:30:04

+0

@AljoshaBre因爲反射非常慢*(特別是在Dalvik上)*。你可以得到一個或兩個殘渣。在這種情況下,我們正在談論100。如果我沒有記錯,'getIdentifier()'也會在內部使用反射,這不會讓事情變得更好。看看[這個問題](http://stackoverflow.com/questions/5492236/why-does-reflection-slow-down-android-phone)和[鏈接的一個](http:// stackoverflow。 COM /問題/ 435553/Java的反射性能)。雖然我會說措施並做出自己的決定。它聽起來像我的經驗中的一個壞主意。 – 2012-08-05 07:53:50

3

你可以試試這個。 YourClassName.class.getFields();

Field[] fields = R.drawable.class.getFields();

你可以遍歷所有字段,您可能需要如果u有比你需要額外的字段進行過濾。

4

resource目錄的values文件夾中創建一個XML文件。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<array name="myIcons"> 
    <item>@drawable/icon1</item> 
    <item>@drawable/icon2</item> 
    <item>@drawable/icon3</item> 
    <item>@drawable/icon4</item> 
    <item>@drawable/icon5</item> 
    ... 
    ... 
</array> 
</resources> 

通過下面的代碼,你會明白。

Resources res = getResources(); 
TypedArray myIcons= res.obtainTypedArray(R.array.myIcons); //mentioned in the XML 
for(int i=0; i<100; i++) 
{ 
    Drawable drawable = myIcons.getDrawable(i); 
    list.add(drawable); 
} 
+0

這是正確的解決方案。使用反射是迄今爲止這樣做的最糟糕的方式。 – Falmarri 2012-08-05 07:11:43

+2

@Falmarri 小心解釋爲什麼? – nullpotent 2012-08-05 07:29:23

+0

嗨!謝謝你的回答,我真的很喜歡它。我唯一的問題是(我沒有在問題中提到它),我需要創建的列表是一個靜態字段,我只能將它上載到靜態初始化塊中,我無法訪問任何上下文實例。在這種情況下的任何想法? :( – 2012-08-05 09:08:46

0

下面是我落得這樣做:

icons = new ArrayList<Integer>(100); 

//get all the fields of the R.drawable class.  
Field [] fields = R.drawable.class.getDeclaredFields(); 

//create a temporary list for the names of the needed variables. 
ArrayList <String> names = new ArrayList<String>(100); 

//select only the desired names. 
for(int i=0; i<fields.length; i++) 
    if(fields[i].getName().contains("icon_")) 
     names.add(fields[i].getName()); 

//sort these names, because later i want to access them like icons.get(0) 
//what means i want icon_0. 
Collections.sort(names); 

try 
{ 
    for(int i=0; i<names.size(); i++) 
    { 
     //get the actual value of these fields, 
     //and adding them to the icons list. 
     int id = R.drawable.class.getField(names.get(i)).getInt(null); 
     icons.add(id); 
    } 
} 
catch(Exception ex) 
{ 
    System.out.println(ex.getMessage()); 
} 

我敢肯定,這不是最快的方式,但它的工作。我會接受AljoshaBre的解決方案,因爲他的回答讓我接觸到這個解決方案。

謝謝大家的幫助!

+0

好吧,但是我希望你意識到在Dalvik上使用反射API的所有優點和缺點 – nullpotent 2012-08-05 10:42:30

+0

這是第二次或第三次,我看到反射API在行動,所以我不知道有什麼好處/缺點,我什至不知道Dalvik的意思,我只是不想使用xml方法,因爲我必須輸入類似於手動添加ID到列表的方式,那麼請您提供一些有關Reflection的信息嗎? – 2012-08-06 17:45:28