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(R.array.name);

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:

 http://stackoverflow.com/questions/2349652/android-open-resource-from-drawable-string/2349758 #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)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

--


Sponsored Links


Variable in getResources().getStringArray

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


 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

--



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)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android 2.0 Programming Books:  http://commonsware.com/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>
</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.






--



Variable in getResources().getStringArray

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


 




did you try:

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

pskink

--



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 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 ite



Other Threads

1. Application Launch - behind the screen

Hi,

I am trying to study the entire application launch process right from
user clicks an activity icon to onCreate and onStart gets called for
the main activity in that application.

In Home application, I found this code

public void onClick(View v) {
        getContext().startActivity((Intent) v.getTag());
}

but startActivity is an abstract method in Context class.

I also tried to trace the startActivity method in Activity class but i
couldn't do it beyond onTransact which involves an IPC call.

Can you please help me with this? I want to trace how a linux process
gets created then how Dalvik VM instance gets created and so on..

Any pointers in this direction would be highly appreciated.

Thank you,
Radhika

--~--~---------~--~----~------------~-------~--~----~

2. View size after orientation change

Hi,

Is the view aware of the orientation change in order to return updated
size after a change in orientation? I'm handling the orientation
myself (android:configChanges="orientation|keyboardHidden"). Does it
means that I have to take into account the orientation state when
querying for view.getHeight()? The view returns the same value as
before the orientation change. It this the normal behavior or I should
"notify" somehow the view to "refresh" its size.

Thanks.

--~--~---------~--~----~------------~-------~--~----~

3. offscreen to onscreen

4. Replaying static AudioTrack

5. Multiple GridView sizing issue

6. how get the possition of View in ListView, that called onCreateContextMenu?

7. Android strength?