开发者

Android - referencing string array using another string with getIdentifier() and getStringArray

开发者 https://www.devze.com 2023-04-07 15:37 出处:网络
I have a variety of string arrays I want to access depending on which one the user decides to use.I don\'t want to use a SQLite DB because I am very new to Android/Java and I have struggled to find ex

I have a variety of string arrays I want to access depending on which one the user decides to use. I don't want to use a SQLite DB because I am very new to Android/Java and I have struggled to find examples so I'm guessing this is a rather poor way to do it but all the same...

If I have in a xml file this:

    <string-array name="bob">
<item>1</item>
<item>4</item>
<item>7</item>
<item>11</item>
</string-array>

And in a Java file this:

String name = "bob";

Why does the following not work?开发者_如何学Go It crashes on startup every time.

int holderint = getResources().getIdentifier("name", "array",
                this.getPackageName());
String[] items = getResources().getStringArray(holderint);


Shouldn't this line be like this?

int holderint = getResources().getIdentifier(name, "array",
                this.getPackageName()); // You had used "name"
String[] items = getResources().getStringArray(holderint);
0

精彩评论

暂无评论...
验证码 换一张
取 消