V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
darktiny
V2EX  ›  Android

[求助] 如何简化Switch代码

  •  
  •   darktiny · 2013-05-24 16:32:41 +08:00 · 9577 次点击
    这是一个创建于 4200 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需要根据天气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;
    }
    11 条回复    1970-01-01 08:00:00 +08:00
    raincious
        1
    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
        2
    batfree  
       2013-05-24 16:57:58 +08:00   ❤️ 1
    两种方法:
    1. R里面的资源编号是有顺序的。找到第一个然后其他的依次+1
    2. 反射,从字符串反射出R.drawable的对应的属性值。
    第一个速度快,第二个保险。
    darktiny
        3
    darktiny  
    OP
       2013-05-24 17:04:28 +08:00
    @raincious 不可以
    darktiny
        4
    darktiny  
    OP
       2013-05-24 17:06:07 +08:00
    @batfree 不是很理解第二种方法要怎么实现
    hfcc8685
        5
    hfcc8685  
       2013-05-24 17:08:07 +08:00
    表模式应该行
    zhangxiao
        6
    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
        7
    expensivefreee  
       2013-05-24 17:50:41 +08:00   ❤️ 1
    qiibeta
        8
    qiibeta  
       2013-05-24 18:04:46 +08:00   ❤️ 1
    int resID = getResources().getIdentifier("ic_weather_"+String.valueOf(code), "drawable",getPackageName());
    Drawable image = getResources().getDrawable(resID);
    darktiny
        9
    darktiny  
    OP
       2013-05-24 18:30:07 +08:00
    @qiibeta 谢谢,非常有用
    darktiny
        10
    darktiny  
    OP
       2013-05-24 18:30:47 +08:00
    @expensivefreee 顺便研究下,谢谢
    xpfd
        11
    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;
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2459 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 15:42 · PVG 23:42 · LAX 07:42 · JFK 10:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.