1

In PHP

$variable_name = "LOREM_IPSUM";
$x = "variable_name";


echo $x;     // Print "variable_name"
echo $$x;    // Print "LOREM_IPSUM"

Java equivalent to get a variable named like the string contained in another variable.

For example to avoid:

 case 0:
     rb0.setEnabled(true);
     break;
 case 1:
     rb1.setEnabled(true);
     break;
 case 2:
     rb2.setEnabled(true);
     break;
...
1

2 Answers 2

0

You can use an Map for instance, where the key is the id and the value the radiobutton then you just have to get on the map to get the radiobutton. Don't forget java is more verbose than PHP. If you need to access the variable by its name you can use reflection https://docs.oracle.com/javase/tutorial/reflect/

Sign up to request clarification or add additional context in comments.

Comments

0

Sounds like you want to use something like eval() in javascript.

I haven't tested it so I don't even know if this will work but maybe you can try something like the ScriptEngine class and evaluate it as a Javascript string.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String result = engine.eval(x);

Look here on how to import it in Android

Comments

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.