[求助] 如何简化Switch代码

2013-05-24 16:32:41 +08:00
 darktiny
需要根据天气Code获取相应图片资源的ID,如何有效地简化?谢谢

/**
* Stupid Code!!!
*
* @param code
* @return
*/
private int getResourceId(int code) {
int resId = 0;
switch (code) {
case 3:
resId = R.drawable.ic_weather_3;
break;
case 4:
resId = R.drawable.ic_weather_4;
break;
case 5:
resId = R.drawable.ic_weather_5;
break;
case 6:
resId = R.drawable.ic_weather_6;
break;
case 7:
resId = R.drawable.ic_weather_7;
break;
case 8:
resId = R.drawable.ic_weather_8;
break;
case 9:
resId = R.drawable.ic_weather_9;
break;
case 10:
resId = R.drawable.ic_weather_10;
break;
case 11:
resId = R.drawable.ic_weather_11;
break;
case 12:
resId = R.drawable.ic_weather_12;
break;
case 13:
resId = R.drawable.ic_weather_13;
break;
case 14:
resId = R.drawable.ic_weather_14;
break;
case 15:
resId = R.drawable.ic_weather_15;
break;
case 16:
resId = R.drawable.ic_weather_16;
break;
case 17:
resId = R.drawable.ic_weather_17;
break;
case 18:
resId = R.drawable.ic_weather_18;
break;
case 19:
resId = R.drawable.ic_weather_19;
break;
case 20:
resId = R.drawable.ic_weather_20;
break;
case 21:
resId = R.drawable.ic_weather_21;
break;
case 22:
resId = R.drawable.ic_weather_22;
break;
case 23:
resId = R.drawable.ic_weather_23;
break;
case 26:
resId = R.drawable.ic_weather_26;
break;
case 27:
resId = R.drawable.ic_weather_27;
break;
case 28:
resId = R.drawable.ic_weather_28;
break;
case 29:
resId = R.drawable.ic_weather_29;
break;
case 30:
resId = R.drawable.ic_weather_30;
break;
case 31:
resId = R.drawable.ic_weather_31;
break;
case 32:
resId = R.drawable.ic_weather_32;
break;
case 33:
resId = R.drawable.ic_weather_33;
break;
case 34:
resId = R.drawable.ic_weather_34;
break;
case 35:
resId = R.drawable.ic_weather_35;
break;
case 36:
resId = R.drawable.ic_weather_36;
break;
case 37:
resId = R.drawable.ic_weather_37;
break;
case 38:
resId = R.drawable.ic_weather_38;
break;
case 39:
resId = R.drawable.ic_weather_39;
break;
case 40:
resId = R.drawable.ic_weather_40;
break;
case 41:
resId = R.drawable.ic_weather_41;
break;
case 42:
resId = R.drawable.ic_weather_42;
break;
case 43:
resId = R.drawable.ic_weather_43;
break;
case 44:
resId = R.drawable.ic_weather_44;
break;
case 45:
resId = R.drawable.ic_weather_45;
break;
case 46:
resId = R.drawable.ic_weather_46;
break;
case 47:
resId = R.drawable.ic_weather_47;
break;
default:
resId = R.drawable.ic_weather_3200;
break;
}
return resId;
}
10222 次点击
所在节点    Android
11 条回复
raincious
2013-05-24 16:37:39 +08:00
能改个形式么?比如:

R.drawable里面加个方法get_ic_weather()。

if (code >= 3) {
resId = R.drawable.get_ic_weather(code);
} else {
resId = R.drawable.get_ic_weather(3200);
}
batfree
2013-05-24 16:57:58 +08:00
两种方法:
1. R里面的资源编号是有顺序的。找到第一个然后其他的依次+1
2. 反射,从字符串反射出R.drawable的对应的属性值。
第一个速度快,第二个保险。
darktiny
2013-05-24 17:04:28 +08:00
@raincious 不可以
darktiny
2013-05-24 17:06:07 +08:00
@batfree 不是很理解第二种方法要怎么实现
hfcc8685
2013-05-24 17:08:07 +08:00
表模式应该行
zhangxiao
2013-05-24 17:46:43 +08:00
R.drawable.ic_weather_N
这些必须这么命名么,如果是:R.drawable.ic_weather.N的形式会不会好点,可以有一个R.drawable.ic_weather._default
expensivefreee
2013-05-24 17:50:41 +08:00
qiibeta
2013-05-24 18:04:46 +08:00
int resID = getResources().getIdentifier("ic_weather_"+String.valueOf(code), "drawable",getPackageName());
Drawable image = getResources().getDrawable(resID);
darktiny
2013-05-24 18:30:07 +08:00
@qiibeta 谢谢,非常有用
darktiny
2013-05-24 18:30:47 +08:00
@expensivefreee 顺便研究下,谢谢
xpfd
2013-05-24 20:38:33 +08:00
int ic_weathe[48] = {R.drawable.ic_weather_3200,R.drawable.ic_weather_3200,R.drawable.ic_weather_3200,R.drawable.ic_weather_3 .... 47};

if(code >= 0) && (code < 48){
return ic_weathe[code];
}else{
return R.drawable.ic_weather_3200;
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/70024

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX