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.
            Asked
            
        
        
            Active
            
        
            Viewed 2,412 times
        
    3 Answers
1
            Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection
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