I 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 R.java (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
package.

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 itemName = "name";
Field resField = R.array.class.getField(itemName);
int getRes = resField.getInt(null);
items = getResources().getStringArray(getRes);

You also have to handle a few exceptions that the reflection code
could throw. It won't, if your code is right, but the compiler will
force you to write code to handle them anyway.

Anyway -- the better way is:

int getRes = getResources().getIdentifier("itemName", "array",
getPackageName()); // getPackageName() gets the current app's Android
package name

... as has been discussed in the other messages. I hope this helps you
put it in context, and gives you a better idea of what to expect from
Java.

This probably actually does something like the reflection code I
showed above, under the hood. However, it could do special stuff to be
faster, or it could cache the result. It's certainly cleaner and
simpler code.



On Mar 1, 4:29 am, Alain <aarn...@gmail.com> wrote:
> 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>
> </string-array>
>
> 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
> item.
>
> 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.
>
> On Feb 28, 7:04 am, Mark Murphy <mmur...@commonsware.com> wrote:
>
>
>
> > Alain wrote:
> > > Mark,
>
> > > 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,
> > > getPackageName());
> > >    items = getResources().getStringArray(itemsid);
>
> > > The itemName[] are  string-array struct in the res/values/arrays.xml
>
> > Try getIdentifier(itemName[...], "array", getPackageName());
>
> > --
> > Mark Murphy (a Commons 
> > Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> > Android 2.0 Programming Books:http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to