HippoMan wrote:
> I know that I can retrieve a string within an Activity as follows:
> 
> this.getString(R.string.foobar)
> 
> (assuming that I have previously defined a string named "foobar").
> 
> However. I'm wondering if there also might be an alternate way to
> retrieve this same string in a functional manner, without an explicit
> attribute reference. I'm talking about something like this:
> 
> this.somehowGetStringAnotherWay("foobar")
> 
> The reason I'd like this is so I can avoid compile errors if the
> string doesn't exist. This would allow me to do something like the
> following:
> 
> String foobar = null;
> try {
>   foobar = this.somehowGetStringAnotherWay("foobar");
> }
> catch (SomeSortOfException e) {
>   Log.e("MyActivity", "foobar string not found");
> }
> 
> Is there any way I can do this?

Resources has getIdentifier() for this. However, this is significantly
less efficient than just using the R static data member. Use it if you
have to (and cache the lookups), but "avoid compile errors" doesn't
strike me as a great reason to do so.

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

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to