usssrrrrrr1
usssrrrrrr1

Reputation: 27

Convert string to object name. I.E. R.string.objectname

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?

Upvotes: 0

Views: 2391

Answers (4)

ryanbrainard
ryanbrainard

Reputation: 6047

I'm guessing on your data types here from what I can see in your code sample, but it looks like you need to use a HashMap<String, Text> to correlate the String to the Text (is that another String?) that you are setting in setText(). This simplify your if/else conditions, be more dynamic, and will be more performant because it is hashed.

I'm not sure where R.string.def1 and R.string.def2 are coming from in your sample, but it you load them in the HashMap like this at the beginning:

Map<String, Text> strToText = new HashMap<String, Text>();
strToText.put("def1", R.string.def1);
strToText.put("def2", R.string.def2);

You can look them up like this when retrieving:

t.setText(strToText.get(str));

You can also do something where R.string itself is a HashMap instead of copying it over (if you have access it change it that is...looks like it might be some Android library). You might also want to consider using Java reflection for doing the lookup and/or initializing the map so you don't have to change your code every time the defs change.

Upvotes: 0

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

str.equals("def1");

you should use equals.

you can access your string by..

String def1String = getResources().getString(R.string.def1);
t.setText(def1String);

getResources() is method on context, or activity.

Upvotes: 2

goto10
goto10

Reputation: 4370

Use Resources.getIdentifier(...) to look up a resource id by name.

public void ShowDefinition(ImageView v) {
    String str=(String) v.getTag();
    setContentView(R.layout.ditionary);
    TextView t = (TextView)findViewById(R.id.definition);
    t.setText(getResources().getIdentifier(str, "string", "com.your.package.name"));

Replace com.your.package.name with the package name of your app.

Upvotes: 5

Prince John Wesley
Prince John Wesley

Reputation: 63688

Use equals method:

if(str.equals("def1")) 
...

or use switch case(if you have java7)

switch(str) {
   case "def1":
     ...
     break;  
}

Upvotes: 0

Related Questions