-3
public static int[] tabCards() {
    return new int[] {
            R.string.title_section1,
            R.string.title_section2
    };
}

I want to fill the int[] array with numbers from 1 to 41. How can I achieve this? I tried using a for loop but I don't know how to implement it exactly in this case.

I want that int[]has title_section1, title_section2, title_section3, title_section4, ... until title_section41.

6
  • You can't parametrize a variable name, so your best bet would be to see if there exist some getter method which takes an index. Commented May 19, 2016 at 0:59
  • I think this is a duplicate but I'll defer to android people: stackoverflow.com/questions/7493287/… Commented May 19, 2016 at 1:00
  • title_section1 to 41 is defined in strings.xml, if that information helps? Commented May 19, 2016 at 1:04
  • This won't work with an initializer, you will have to create the array as an local variable and iterate over it. Commented May 19, 2016 at 2:55
  • If your're talking about strings how about modifying your strings.xml to have R.string.title_section1 -> ...title_section41 Commented May 19, 2016 at 3:56

2 Answers 2

3

Disclaimer: I'm not an Android guy. But based on this answer, you should be able to do this:

public static int[] tabCards() {
    int result = new int[41];
    for (int i = 0; i < result.length; i++) {
        result[i] = getResources().getIdentifier("title_section" + i, "string", getPackageName());
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for you try, but this doesn't work. getResources cannot be referenced from a static context
@Vaia So... make it non-static?
@Vaia Did you save and refresh? Are you in a static inner class?
Simply pass the context to tabCards(Context ctx) and use it so result[i] = ctx.getResources()...
0
public static int[] tabCards() {
    return new int[] {
        R.string.title_section1,
        R.string.title_section2,
        R.string.title_section3,
        R.string.title_section4,
        R.string.title_section5,
        R.string.title_section6,
        R.string.title_section7,
        R.string.title_section8,
        R.string.title_section9,
        R.string.title_section10,
        R.string.title_section11,
        R.string.title_section12,
        R.string.title_section13,
        R.string.title_section14,
        R.string.title_section15,
        R.string.title_section16,
        R.string.title_section17,
        R.string.title_section18,
        R.string.title_section19,
        R.string.title_section20,
        R.string.title_section21,
        R.string.title_section22,
        R.string.title_section23,
        R.string.title_section24,
        R.string.title_section25,
        R.string.title_section26,
        R.string.title_section27,
        R.string.title_section28,
        R.string.title_section29,
        R.string.title_section30,
        R.string.title_section31,
        R.string.title_section32,
        R.string.title_section33,
        R.string.title_section34,
        R.string.title_section35,
        R.string.title_section36,
        R.string.title_section37,
        R.string.title_section38,
        R.string.title_section39,
        R.string.title_section40,
        R.string.title_section41
    };
}

1 Comment

I know that it works this way, but that's why I asked for a for loop.

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.