2

If I know a variable's pattern such as R.id.edit_x where x (1..N), how can I get a reference to a given EditText, like findViewByID(R.id.edit_1). Is there something like an "eval" function in Dalvik ? Thanks.

xain
  • 13,159
  • 17
  • 75
  • 119

3 Answers3

1

Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection

Community
  • 1
  • 1
hoha
  • 4,418
  • 17
  • 15
0

maybe, you can check roboguice. it is a ioc framework for android and it's realy easy to use. i copy some code from the sample from the project to show how to use it:

public class AstroboyMasterConsole extends RoboActivity {

    @InjectView(R.id.self_destruct) Button selfDestructButton;
    @InjectView(R.id.say_text)      EditText sayText;
    @InjectView(R.id.brush_teeth)   Button brushTeethButton;
    @InjectView(tag="fightevil")    Button fightEvilButton;     // we can also use tags if we want

}

then you can you these injected variables in your code!

Jet Geng
  • 347
  • 1
  • 7
0

hoha's answer is good. Another thing you can do is create a look-up table that maps 1..N to the resource IDs. (Presumably you know all the resource IDs ahead of time.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Actually the EditTexts are created programmatically so no IDs are previously known. – xain Mar 13 '11 at 11:32