2011-12-30 26 views
0

嘿,我得到這個XML數組:塞汀使用color.xml顏色 - 一個字符串數組

<resources> 
    <string-array name="colors">   
     <item>BLUE</item> 
     <item>CYAN</item> 
     <item>DARK_GRAY</item> 
     <item>GRAY</item> 
     <item>GREEN</item> 
     <item>LIGHT_GRAY</item> 
     <item>MAGENTA</item> 
     <item>ORANGE</item> 
     <item>PINK</item> 
     <item>RED</item> 
     <item>YELLOW</item> 
     <item>WHITE</item> 
    </string-array> 
</resources> 

我試圖這樣簡單的方法,這顯然不能工作:

public int[] getColorsArray(int i) {   
    int[] allColors = MyApplication.getContext().getResources().getIntArray(R.array.colors); //this is probably wrong 
    int[] array = new int[i]; 

    for (int j = 0; j < array.length; j++) { 
     array[j] = allColors[j]; //this is wrong 
    }  

    return array; 
} 

}

有沒有辦法使用這樣的XML數組?

+0

你想完成什麼? – jagsler 2011-12-30 13:01:35

+0

實際上,我需要一個圖表系列的顏色數組。我想從allColors數組中挑選必要的顏色。 – 2011-12-30 13:05:13

+0

好的,有什麼錯誤? – jagsler 2011-12-30 13:09:25

回答

0

好的,我以另一種方式工作。

private int[][] allColors() { 

int rgbArray[][] = { 
{3,48,208},  //blue  
{73,33,65},  //blue  
{63,92,202},  //blue  
{107,134,233}  //blue (yea, they are all different but I'm no woman) 
}; 

return rgbArray; 
} 

public int[] getColorsArray(int i) {   
    int[][] allColors = allColors(); 
    int[] array = new int[i]; 

    for (int j = 0; j < array.length; j++) { 
     array[j] = Color.rgb(allColors[j][0], allColors[j][1], allColors[j][2]); 

    }  

    return array; 
} 
0

我會使用2個數組,一個字符串數組作爲名稱,一個int數組作爲顏色值。

1

問題是in xml you have defined string-array but in program you are trying to get int-array。使用getStringArray並檢查結果

String[] allColors = getResources().getStringArray(R.array.colors); //this is probably wrong 
     String[] array = new String[allColors.length]; 

     for (int j = 0; j < allColors.length; j++) { 
      array[j] = allColors[j]; //this is wrong 

      System.out.println(j+"...j..."+allColors[j]); 
     } 
相關問題