0

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.

1
  • You can have an array of String. Commented Jun 18, 2017 at 10:11

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

How can we declare HashMap as public class?

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.