내가 자꾸 까먹어서 쓰는 개발 이야기/Android
변수를 사용해서 findViewById
FIL.
2015. 4. 21. 11:36
728x90
ViewFlipper 등에서 사용하면 매우 유용할 것 같다.
getIdentifier의 세번째 인수에는 getPackageName()이 유용할 듯 싶다.
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(buttonID, "id", "com.sample.project");
buttons[i][j] = ((Button) findViewById(resID));
buttons[i][j].setOnClickListener(this);
}
}
출처: http://stackoverflow.com/a/4865350/4531618