I am working on app where I am loading weather from Yahoo. From their XML I am getting "text", "code", "temp". What I want to do is based on code load image and display it. Code range is from 00-47 and I use those amazing icons . Now I know I can load image using this way:
if (code == 0){
ImageView imageDisplay = (ImageView) findViewById(R.id.weatherImg);
imageDisplay.setImageResource(R.Drawable.weather_00)
} else if (code == 1) {
}
But this is not a great solution, than I have 100+ lines of code of if else statement. My idea was to do it this way:
for(int i = 0;i<48;i++){
if(weatherCode == i){
String imgName = "R.Drawable.weather_" + i;
}
but I cannot use String with "Drawable" it has to be Drawable... Does anybody know how to dynamically change name of the image based on a code I am using?
I've resolved it this way:
String imgName = "weather_" + i;
ImageView imageDisplay = (ImageView) findViewById(R.id.weatherImg);
imageDisplay.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(imgName, "drawable", getPackageName())));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.