Created
March 14, 2012 20:30
-
-
Save wolfiestyle/2039291 to your computer and use it in GitHub Desktop.
assign all views on R.id to member variables of the same name using reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** assign all views on R.id to member variables of the same name using reflection */ | |
private void findAllViews() | |
{ | |
Field[] r_fields = R.id.class.getDeclaredFields(); | |
for (Field f: r_fields) | |
{ | |
String name = f.getName(); | |
Field v; | |
try | |
{ | |
v = this.getClass().getDeclaredField(name); | |
} catch (Exception e) | |
{ | |
Log.e("findAllViews", "field not found: " + e.getMessage()); | |
continue; | |
} | |
int value; | |
try | |
{ | |
value = f.getInt(null); | |
} catch (Exception e) | |
{ | |
continue; | |
} | |
if (value == 0) | |
continue; | |
try | |
{ | |
v.set(this, this.findViewById(value)); | |
} catch (Exception e) | |
{ | |
Log.e("findAllViews", "error setting value: " + e.getMessage()); | |
continue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment