0

I want to do something similar to this as explained in Reference drawable from string-array in android

private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

I tried this .java file :

    int[] descriptions = new int[] { 
        R.string.description_section1, 
        R.string.description_section2, 

    }; 

but it doesn't seem to be the right syntax.

4
  • May I ask you what's wrong in using a strin array as a reource? You'd have the array already set in a resource file... Commented Apr 27, 2014 at 9:08
  • I want to reference in CODE and make a loop with the array Commented Apr 27, 2014 at 9:12
  • You can do so also with a string array set in xml and then retrieved in code... Commented Apr 27, 2014 at 9:14
  • I don't want to do so in XML I want to use code like in this post stackoverflow.com/questions/19791845/… Commented Apr 27, 2014 at 9:15

2 Answers 2

3

define your array in array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="descriptions">
        <item>section1</item>
        <item>section1</item>
    </string-array>
 </resources>

then try this:

String[] descriptions = getResources().getStringArray(
                R.array.descriptions);
Sign up to request clarification or add additional context in comments.

2 Comments

... Exactly what I proposed.
Thanks for the alternative though I'm not fond of XML
2

Try this as mentioned in the docs:

int[] descriptions = { 
    R.string.description_section1, 
    R.string.description_section2, 

}; 

1 Comment

@BobMalooga Because this compiles just fine, I think the extra comma at the last element is optional. But I don't know where this is specified.

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.