this is a question about java on android - eclipse
I have a String
r
for the record, I defind him like that:
String r="pic"+c1;
I want to take the value of it, which is "pic6" and use it as a variable name.
how do I do it?
this is a question about java on android - eclipse
I have a String
r
for the record, I defind him like that:
String r="pic"+c1;
I want to take the value of it, which is "pic6" and use it as a variable name.
how do I do it?
If you want to pick an image with name "pic6.png" from the drawable folder of the Android application, you can try the following code.
String r = "pic" + c1;
int resId = getResources().getIdentifier(r, "drawable", getPackageName());
if (resId != 0) {
imageView.setImageResource(resId);
} else {
// This is used in case no image resource is found
imageView.setImageResource(R.drawable.unknown);
}
Please see: If you use the else part, then you need to add "unknown.png" image to the drawable folder of the Android project.
Hope this helps.
You can't do it directly but you could use a HashMap to emulate what you seek.
Example:
String r="pic"+c1;
HashMap<String,Integer> extendedVars=new HashMap<>();
extendedVars.put(r,10);
In java, variable names must be known in compile time. There is no way to do what you are trying to do.