I am having trouble figuring out how to use a sting as an object name. Let me explain. I have a dynamically assigned image which I tag. I get that tag as a string and I have a stored string (a definition) with the same name as the tag. I wan’t to be able to SetText() using the tag which is the same as the sting name. Here is what I am trying to achieve:
public void ShowDefinition(ImageView v){
String str=(String) v.getTag();
setContentView(R.layout.ditionary);
TextView t = (TextView)findViewById(R.id.definition);
if(str == "def1")
t.setText(R.string.def1);
if(str == "def2")
t.setText(R.string.def2)
}
The if statements are what I would like to do in a simpler way. The string str is a string pulled from the tag. The android resource strings have the same name, as you can see, but I can not figure out any way to use str in the setText argument. Any Ideas?
Use Resources.getIdentifier(…) to look up a resource id by name.
Replace
com.your.package.namewith the package name of your app.