[英]Yahoo weather and Android
我正在开发从Yahoo加载天气的应用程序。 从他们的XML中,我得到了“文本”,“代码”,“临时”。 我要基于代码加载图像并显示它。 代码范围是00-47,我使用那些了不起的图标 。 现在我知道我可以使用这种方式加载图像:
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与“ Drawable”一起使用,它必须是Drawable ...有人知道如何根据我使用的代码动态更改图像的名称吗?
我已经这样解决了:
String imgName = "weather_" + i;
ImageView imageDisplay = (ImageView) findViewById(R.id.weatherImg);
imageDisplay.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(imgName, "drawable", getPackageName())));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.