If you want to 'dynamically' load these strings, you probably could
use reflection.

Some 'pseudo' code:

public String somehowGetStringAnotherWay(Resources res, String
stringName) {
  try {
    Field stringField = R.string.class.getField(stringName);
    int stringID  = stringField.getInt(null);
    return res.getString(stringID);
  }
  catch (Exception e) {
    return null;
  }
}


However, doing this to 'avoid compilation errors' does not sound like
a good idea...

On Mar 18, 9:49 pm, HippoMan <hippo.mail...@gmail.com> 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?
>
> Thanks in advance.

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