0

Hey I got this xml array:

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

I was trying such simple method which obviously can't work:

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

}

Is there a way to use such xml array?

3
  • what are you trying to accomplish? Commented Dec 30, 2011 at 13:01
  • actually I need colors array for a chart serie. I want to pick necessary ammount of colors from allColors array. Commented Dec 30, 2011 at 13:05
  • yeah well, getIntArray(R.array.colors) always gets 0; which is not weird as the xml array is an string array. I think I'm gonna do this another way Commented Dec 30, 2011 at 13:12

3 Answers 3

1

The problem is that in xml you have defined string-array but in program you are trying to get int-array. Use getStringArray and check the results

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]);
        }  
Sign up to request clarification or add additional context in comments.

Comments

0

I would use 2 arrays, one string-arrays for the names and one int arrays for the colors values.

Comments

0

Ok I worked this out another way.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.