1

I have a method that returns one of about 20 possible strings from an EditText. Each of these strings has a corresponding response to be printed in a TextView from strings.xml. Is there a way to call a string from strings.xml using something like context.getResources().getString(R.strings."stringFromMethod")? Is there another way to call a string from a large list like that?

The only methods I can think of is converting each string to an int, and use that to find a string in a string array, or a switch statement. Both of which involve a huge amount if-else if statements to convert the string to an int, and would take just enough steps to change if any strings were added or taken away that I'd be more likely to miss one and have fun bug hunting. Any ideas to do this cleanly?

Edit: Forgot to add, another method I tried was using was to get the resourceID from

int ID = context.getResources().getIdentifier("stringFromMethod", "String", context.getPackageName())

and taking that integer and putting it in

context.getResources().getString(ID)

That doesn't appear to be working either.

2
  • Could you give the purpose? Maybe there is another way to solve a problem. Commented Jul 8, 2012 at 6:23
  • I'm toying with a text based game. The user can input a text command, and a response is posted in a TextView. Unfortunately, there never was a point that there wasn't a string, which was where I ran into trouble. Commented Jul 8, 2012 at 6:29

3 Answers 3

5

No, you can't. The getString() requires the resource id in integer format, so you can't append a string to it.

You can, however, try this:

String packageName = context.getPackageName();
int resId = context.getResources().getIdentifier("stringFromMethod", "string", packageName);
if (resId == 0) {
    throw new IllegalException("Unknown string resource!"; // can't find the string resource!
}
string stringVal = context.getString(resId);

The above statements will return string value of resource R.string.stringFromMethod.

Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to mention it, but I did just that (edited it in, guess I wasn't in time). It wasn't working, so I assumed it was not a valid way to do that. If it is, there must be something else wrong with my code. I'll look into it.
Well, that was fast and annoyingly simple. I used "String" instead of "string" as the defType. Thanks for the example, that solved it.
0

You need to use reflection (pretty ugly but only solution) load the R class, and get the relevant field by you string and get the value of it.

Comments

0

this is what I used to do in these kind of situations, I will made a Array like

int[] stringIds = { R.string.firstCase,
        R.string.secondCase, R.string.thridCase,
        R.string.fourthCase,... };


int caseFromServer=getCaseofServerResponse();
here caseFromServer varies from 0 to wahtever

and then simply

context.getResources().getString(stringIds[caseFromServer]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.