1

I ma making a game, and I want to pull the apprpriate dialog from the resource file, but which string to pull changes from level to level.

How would I get this to work?

I would want something like:

int level = 3;
String dialog = getResources().getString(R.string.["level_dialog_"+level]);

With the goal being that the apprpriate string resource ID is found using the name of the string resource plus the level. But of course that won't even compile.

What is the right way to do this?

2 Answers 2

2

Thanks AJcodez for getting me thinging along the right path. Even if you answer was not the right one.

I found the right answer myself:

To get a resource by string name, there is already an android function as part of the Resources object:

int id =  getResources().getIdentifier("level_"+1+"_intro", "array", getPackageName());
String[] level1 = getResources().getStringArray(id);

The first line, I get the resource id. The parameters are the name of the resournce, the type (array, id, string, drawable, etc) and the third parameter is the package name.

In the second line, I can now get the resource just like I would using R.array.

(note to self: RTFM)

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

Comments

1

I would store your strings in a string-array, and then access the strings through the index of the array. See here: http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

2 Comments

Well, maybe I should have made a better example. Because the case I actually have is an array for each level. Can I make an array of string arrays?
In this case you will probably need to serialize your data, and put that in the array. I would just use toString() and make a quick method to take a string representation of an array and put the array back together. Or you could store array of arrays in Java in a static class. Probably easier if it is never changed.

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.