mpsbhat
mpsbhat

Reputation: 2773

Selecting a value from android resource

In javascript, we can conditionally select a value from a object using a variable as,

var myObj = {"a": "Val1", "b": "Val2", "c": "Val3"};
var key = "a";
var val = myObj[key];

What is the similar method to select the value from a string resource file in android using variable string name?

We can select a string as R.string.mystring in android and from a java public class

public class myarray {
    public static final String test = "Test";    
    public myarray(){    
    }
}

as String val = myarray.test; but we wanted a method something similar to above javascript.

Upvotes: 0

Views: 53

Answers (1)

Sami Kanafani
Sami Kanafani

Reputation: 15741

A translation of your javascript code is the following:

HashMap<String, String> myObj = new HashMap<String, String>();
myObj.put("a","Val1");
myObj.put("b","Val2");
myObj.put("c","Val3");
String key = "a";
String value = myObj.get(key);

Upvotes: 2

Related Questions