Variable in getResources().getStringArray

by Alain » Sun, 28 Feb 2010 09:51:55 GMT

Sponsored Links
 Instead of accessing items in a string array directly
         items = getResources().getStringArray(;

Can a variable be used in getString array as shown below

String itemName = "name";
String getRes = "R.array."+ itemName;
items = getResources().getStringArray(getRes);


Variable in getResources().getStringArray

by Mark Murphy » Sun, 28 Feb 2010 09:55:11 GMT


Curiously, I just answered a related question seconds ago on StackOverflow: #2349758

Given itemName, you can get the integer resource ID via the
getIdentifier() method on the Resources object.

This uses reflection, apparently, and therefore is comparatively
expensive. Avoid it if possible, cache it if you need to use it.

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


Sponsored Links

Variable in getResources().getStringArray

by Alain » Sun, 28 Feb 2010 11:18:26 GMT


I tried your suggestion with this code, but this throws an exception.

String varName = "R.array."+itemName[listaa_position];
        int itemsid=getResources().getIdentifier(varName, null,
        items = getResources().getStringArray(itemsid);

The itemName[] are  string-array struct in the res/values/arrays.xml


Variable in getResources().getStringArray

by Mark Murphy » Sun, 28 Feb 2010 20:04:29 GMT


Try getIdentifier(itemName[...], "array", getPackageName());

Mark Murphy (a Commons Guy)  | 

Android 2.0 Programming Books: 


Variable in getResources().getStringArray

by Alain » Mon, 01 Mar 2010 20:29:12 GMT

 This does not work either, the returned ID is always 0. The items for
which I am trying to retrieve an ID are in res/values/array.xml and
defined as:

<string-array name="itemName">
   <item> value1 @ value2  </item>
   <item> value3 @ value3  </item>

The user would have selected from another string-array one of the
itemName, the code knows the itemName and I would like to insert it
into a getResources().getStringArray to retrieve more info on the

I tried the following with different combination of packageName but
either crash or return id = 0
String packageName = "com.coname.appname:itemName";
int itemsid=getResources().getIdentifier( packageName, null, null);

The version below also returns 0.
int itemid1 = getIdentifier("R.array."+itemName[listaa_position],
"array", getPackageName());

Thanks for any pointers.


Variable in getResources().getStringArray

by skink » Mon, 01 Mar 2010 21:35:08 GMT


did you try:

getIdentifier("itemName", "array", getPackageName()) ?



Variable in getResources().getStringArray

by Bob Kerns » Tue, 02 Mar 2010 01:10:03 GMT

  think we need to help you clear up your model of the language. I've
been trying to figure out what computer language is your main
language, but I can't quite figure it out -- but it's not Java, I
don't think. If you tell us, maybe we can explain a bit better, but
I'll give it a shot anyway.

At least, I think language difference is why you keep trying to do
things with "R.array" and the '+'operator.

When the documentation mentions R.array, it is naming a specific class
-- one that is generated from the resources. R.array.itemName is the
name of a field within that class -- known in some languages as a
class variable, or a static variable. In Java, it is declared using
the 'static' keyword, and officially call it a 'static field', but you
may well see the other terms in use as well.

Now, one thing that is confusing right off the bat is that the
convention in Java is to start class names with a capital letter, and
'array' violates that convention.

Second thing that may be confusing is that "." in the middle. You've
seen things like com.mypackage.MyClass. The "." character is also
used, however, to separate a class, and an inner class defined within
that class. It's actually possible to import R.array directly, and
refer to just array.itemName -- but please don't do that, you'll
really confuse everyone. You can even import itemName directly, with
the static keyword, to really confuse people.

Now, while in Java, unlike C or C++, the system does know the name of
classes, their package names, and all the fields, still, you can never
execute a string like "R.array.itemName". There is no equivalent to
the Javascript evaluate() or Lisp's (eval) functionality. Java is
purely a compiled language. That means, the exact code to execute has
to already be there when the compiler compiles it. There is a separate
API (in the java.lang.reflect package) that lets you make use of this
information, which the compiler carefully saves for the runtime.

But you cannot construct code at runtime and execute it. Since when
the documentation is talking about R.array, it's giving a code
example, there's no way you can write code that talks about "R.array"
and have it do anything similar.

The FINAL confusing point -- when we're talking about resources, and
refer to package name, we're talking about Android packages, rather
than Java package. This is what you'd probably call an 'app', and the
"package name" referred to is declared in the manifest. The only real
connection between that package name and the Java language, is that
the aapt resource-bundling tool generates (the file with the R
and R.array classes) in that Java package. So, while they happen to be
the same string, as a result -- they're not actually related, and
there's no need or reason to put the Android package name and the
class name together. The resource API is expecting an int value, as
the value of R.array.itemName, and it is expecting a string as the

So, going back to your original idea:

String itemName = "name";
String getRes = "R.array."+ itemName;
items = getResources().getStringArray(getRes);

The last step is right, but you need to look up the value of
R.array.itemName. You can (but should not, since there's a better way)
use Java reflection to do this. I only show you how to illustrate how
your idea would work.

String ite

Other Threads

1. GPS application gets killed on screen timeout

You should try to post on the android-porting forum, it doesn't seem
like you're targetting the G1.

To be more constructive, don't you simply need to hold a WakeLock?


2. How to deploy and bundle third party dynamic library

If the application needs to use a third party dynamically linked
library (.so) file,   how to package that as bundle?.  Can i follow
the  samples platform library example?   The .so file is built with
android toolchain. Will that work even if i have no source code.


3. Emulator not finding java/javascript html program

4. Looking for an Application that will work with OpenDMTP and Open GPS for a Google G1

5. Updated list of app code sources?

6. correct way to use getSystemService?

7. Calling web services in android