0

:)

Im using Java in Android Studio and trying to access a resource file (/res/values/nutrients.xml).

In the nutrients.xml, there are multiple integer-arrays (nut1, nut2, nut3...) :

I have already managed to access the data with this code:

    Resources r = ctx.getResources();
    int[] food = r.getIntArray(R.array.nut1);
    return food[1];

Now i want to do this dynamically, changing the array (before nut1) depending on an integer. I will be providing an integer for that function, that specifies which array im looking for.

I thought that I could just do something like this:

    public int getNutrientInfo(int foodNo, int nutType){
    Resources r = ctx.getResources();
    String arrayName = "R.array.nut" + foodNo;
    int[] testy = r.getIntArray(arrayName);
    return testy[nutType];}

That does not work. I have tried multiple things but can´t find a solution. I am quite sure it is fairly easy though.

Do you have a solution?

Thank you for your help :)

Ni Ho
  • 1
  • 1
  • See https://stackoverflow.com/questions/6583843/how-to-access-resource-with-dynamic-name-in-my-case. – CoolMind Jul 08 '21 at 18:54
  • Thanks! I have tried that already, forgot to mention it. It went something like this, but with that output == 0. What am I doing wrong? int id = getResources().getIdentifier(foodNo, "values", ctx.getPackage); – Ni Ho Jul 08 '21 at 18:58
  • https://stackoverflow.com/a/21829761 – Mike M. Jul 08 '21 at 19:00
  • 1
    It worked. I dont know why I couldnt find/do that myself. Thank you! – Ni Ho Jul 08 '21 at 19:09

0 Answers0