1

I'm developing a java Android app and i'm having a XML-Resource, it looks like this:

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

    <!-- first pattern -->
    <integer-array name="pattern_a">
        <item>1</item>
        <item>0</item>
        <item>1</item>
    </integer-array>

    <!-- second pattern -->
    <integer-array name="pattern_b">
        <item>1</item>
        <item>1</item>
        <item>0</item>
    </integer-array>

    <!-- broad configuration -->
    <string        name="config_broad_name">broad</string>
    <integer-array name="config_broad_pattern">@array/pattern_a</integer-array>

    <!-- small configuration -->
    <string        name="config_small_name">small </string>
    <integer-array name="config_small_pattern">@array/pattern_b</integer-array>

</resources>

I'm creating a Configuration and set some values from my resource files, which seems easy straight forward...

public Configuration createConfiguration(){

    Configuration configBroad = new Configuration();

    String configName = getResources().getString(R.string.config_broad_name);
    int[] pattern = getResources().getIntArray(R.array.config_broad_pattern);

    configBroad.setName(configName);
    configBroad.setPattern(pattern);

    return configBroad;
}

Problem: int[] pattern is an empty int[] (like int[]{})

what would really help, something like this:

int patternId = getArrayId(getResource(), R.string.config_broad_pattern);
configBroad.setPatternId(patternId); //setting just the id, not the whole pattern

then i could retrive the value itself, or even better - set the id on my config instead of the array.

i know, there a ton's of explanations on how to work on this problem with string[] (like array_of_array) and @image/...-Arrays but i couldn't find any one with integer array (especially when looking for Typed-Array i couldn't find help)

Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • try `getResources().getIntArray(R.array.config_broad_pattern);` – Raghunandan Mar 27 '14 at 06:31
  • no, this won't help - i'm trying to create configurations generic! Configurations will be created by name (like 'broad' or 'small') and the related resources will be created by that name "int id = res.getIdentifier(createGenerciName("small", "string", packageName);" – Martin Frank Mar 27 '14 at 06:35
  • uppsi - you won't believe it - but in my original code it IS getIntArray(R.array.config_broad_pattern). sorry, i copy/pasted it wrong, i will right now correct my uestion, thanks for noticing!! – Martin Frank Mar 27 '14 at 06:39
  • 1
    anyway that should show you a error if you use an IDE. But `android.content.res.Resources$NotFoundException` is thrown when android does not find the resource. So that indicates you are referring to a resource that does not exist. post the stacktrace – Raghunandan Mar 27 '14 at 06:41
  • the error posted IS from my console - really, i can get the ressource id of R.string.config_broad_pattern, "int id = res.getIdentifier(idName, "array", packageName);" //returns 0x7f0500012 but using this id to get the array "int[] val = getResource().getIntArray(id);" it throws that exception – Martin Frank Mar 27 '14 at 06:49
  • Well, thank you Raghunandan for your help so far, it helped me to clear out where the problem is... – Martin Frank Mar 27 '14 at 06:55
  • post it as an answer and accept the same. will help others who visit the post. – Raghunandan Mar 27 '14 at 06:56
  • not yet solved, i adapted the problem - the int[] is still empty when you define the array in such way in the xml-resource... i gave your answer a +1 i don't know how else i can value your input - because you did deliver very good solutions!! – Martin Frank Mar 27 '14 at 06:58

2 Answers2

0

OK, it took me lots of time to solve this issie...

when you want to refer to another defined value from your XML, you have to refer to that value as a STRING, at least if you follow my solution...

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

    <!-- first pattern -->
    <integer-array name="pattern_a">
        <item>1</item>
        <item>0</item>
        <item>1</item>
    </integer-array>

    <!-- second pattern -->
    <integer-array name="pattern_b">
        <item>1</item>
        <item>1</item>
        <item>0</item>
    </integer-array>

    <!-- broad configuration - notice difference on reference -->
    <string        name="config_broad_name">broad</string>
    <string name="config_broad_pattern">@array/pattern_a</string>

    <!-- small configuration -->
    <string        name="config_small_name">small </string>
    <string name="config_small_pattern">@array/pattern_b</string><!-- same here -->

</resources>

of course we use that same code as above but we must slightly adjust it some places:

public Configuration createConfiguration(){

    Configuration configBroad = new Configuration();

    String configName = getResources().getString(R.string.config_broad_name);
    String refPatternName = getResources().getString(R.string.config_broad_pattern);
    refPatternName = refPatternName.replace("@",""); //removing @-prefix
    int refPatternId = Integer.parseInt(str); //this value now is the same as R.array.pattern_a


    configBroad.setName(configName);
    configBroad.setPatternId(refPatternId);

    return configBroad;
}
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • I wonder why i can always find a solution AFTER i post a question - honestly i spend two days on finding that solution, and i found it right after i posted the question... thank you very much again Raghunandan !!! – Martin Frank Mar 27 '14 at 07:24
-2

For me this is a simple question. In XML just use string-array and then in java code cast it to integer that all.

KaJasB
  • 2,325
  • 2
  • 11
  • 10
  • this is definitely wrong answer - if you want to build a structure with resources you have to create references and create objects in a generic way - it's not just "getResource().getString(R.string.my_string);" or "getResource().getIntArray(R.array.my_array);" – Martin Frank Mar 27 '14 at 07:35
  • i alway use this. XML: 0 Java: String [] arr=getResource().getStringArray(R.array.arrName); and then cast arr[index] to integer. – KaJasB Mar 27 '14 at 07:48
  • i know i know... but look at the resource file i posted: i want to use and RE-USE a defined array (in my post: pattern_x) - and if you want build twenty or more configurations and create them generic (maybe create a config by a certain name like 'broad') you want to use bigger patterns .... – Martin Frank Mar 27 '14 at 07:57
  • Sorry about that. I'm just a beginner android developer maybe i need to learn more... but can you share me a link to learn how to build animate in android? like a walking character to input in my assignment. – KaJasB Mar 28 '14 at 00:46