Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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? It crashes on startup every time.

int holderint = getResources().getIdentifier("name", "array",
                this.getPackageName());
String[] items = getResources().getStringArray(holderint);
like image 537
KRL Avatar asked Sep 27 '11 06:09

KRL


1 Answers

Shouldn't this line be like this?

int holderint = getResources().getIdentifier(name, "array",
                this.getPackageName()); // You had used "name"
String[] items = getResources().getStringArray(holderint);
like image 186
Rahul Choudhary Avatar answered Nov 07 '22 03:11

Rahul Choudhary