마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

반복문 줄이려고 합니다.

0 추천

현재 제가 코드 정리를 해보려고 하는데요

그 코드중에
 
 
public static BitmapDrawable getSeasonBitmapDrawble(Context context, String str_season){
 
BitmapDrawable img_season = null;
if(str_season.equals("13")){
   img_season =BitmapDrawable)context.getResources().getDrawable(R.drawable.year13);
}else if(str_season.equals("15")){
   img_season =BitmapDrawable)context.getResources().getDrawable(R.drawable.year15);
}else if(str_season.equals("14")){
   img_season =BitmapDrawable)context.getResources().getDrawable(R.drawable.year14);
}
        return img_season; 
}
 

이런 코드가 있습니다.

 
str_season.equals("13") 이 부분은
 
String str_season = spinner.getSelectedItem().toString();
이렇게 해서 가져오구요.
 
코드는 다 반복되고 달라지는 건 값뿐들이거든요.
 
저거를 if 문으로 하려니 너무 길어져서요.
저걸 어떻게 간단하게 개선할 수 있을까요?
브루스웨인 (8,580 포인트) 님이 2015년 6월 21일 질문

1개의 답변

0 추천
 
채택된 답변
String drawableName = "year" + str_season;

int drawableResourceId = this.getResources().getIdentifier(drawableName, "drawable", this.getPackageName());
Gradler (109,780 포인트) 님이 2015년 6월 22일 답변
브루스웨인님이 2016년 8월 3일 채택됨
Int로 받아서 쓰나요??
return ( BitmapDrawable)context.getResources().getDrawable(drawableResourceId);

수저로 떠서 입까지 가져다 드려야 하는군요;;
...