雅虎天气和Android

时间:2014-04-15 13:33:57

标签: java android

我正在开发应用程序,我正在从雅虎加载天气。从他们的XML我得到" text"," code"," temp"。我想要做的是基于代码加载图像并显示它。代码范围是00-47,我使用those amazing icons。现在我知道我可以用这种方式加载图像:

if (code == 0){
    ImageView imageDisplay = (ImageView) findViewById(R.id.weatherImg);
    imageDisplay.setImageResource(R.Drawable.weather_00)

} else if (code == 1) {
}

但这不是一个很好的解决方案,因为我有100多行if else语句代码。我的想法就是这样做:

for(int i = 0;i<48;i++){
    if(weatherCode == i){
         String imgName = "R.Drawable.weather_" + i;
    }

但我不能使用String&#34; Drawable&#34;它必须是Drawable ...有没有人知道如何根据我使用的代码动态更改图像的名称?

1 个答案:

答案 0 :(得分:0)

我已经这样解决了:

String imgName = "weather_" + i;
ImageView imageDisplay = (ImageView) findViewById(R.id.weatherImg);
imageDisplay.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(imgName, "drawable", getPackageName())));
相关问题