关于Android R文件的坑。本地图片资源id存了数据库,一开始显示正常,后来都乱序了。

项目要做一个效果,需要展示对应的数据,名字带图片,数据本地化后,图片id存入数据库,一开始显示正常,后来同事用了一段时间,突然发现图片都特么的乱了。

顿时我的心也乱了。。。   痛苦过后还得面对现实,解决方案当然是有的啦,灵光一现。在帮助类里这样写。

private static Map appIconIdMap = new HashMap<>();

    static {
        // 考勤
        appIconIdMap.put(APP_NAME_1, R.drawable.new_office_my_signlog_icon);
        appIconIdMap.put(APP_NAME_2, R.drawable.new_office_my_signcount_icon);
        appIconIdMap.put(APP_NAME_3, R.drawable.new_office_my_go_out_icon);
        appIconIdMap.put(APP_NAME_4, R.drawable.new_office_my_leave_icon);
        appIconIdMap.put(APP_NAME_5, R.drawable.new_office_my_phone_sign_icon);
        appIconIdMap.put(APP_NAME_6, R.drawable.new_office_my_go_out_manage_icon);
        appIconIdMap.put(APP_NAME_7, R.drawable.new_office_my_go_out_sign_icon);
        appIconIdMap.put(APP_NAME_8, R.drawable.new_office_my_phonesign_out_timer_icon);
        appIconIdMap.put(APP_NAME_9, R.drawable.new_office_my_overtime_icon);
        appIconIdMap.put(APP_NAME_10, R.drawable.new_office_my_retroactive_icon);
        // 流程
        appIconIdMap.put(APP_NAME_11, R.drawable.new_make_mine_apply_icon);
        appIconIdMap.put(APP_NAME_12, R.drawable.new_make_wait_process_icon);
        appIconIdMap.put(APP_NAME_13, R.drawable.new_make_termination_process_icon);
        appIconIdMap.put(APP_NAME_14, R.drawable.new_make_notify_icon);
        appIconIdMap.put(APP_NAME_15, R.drawable.new_make_draft_box_icon);
        appIconIdMap.put(APP_NAME_16, R.drawable.new_make_process_play_icon);
        // 财务
        appIconIdMap.put(APP_NAME_17, R.drawable.new_office_my_loanv_icon);
        appIconIdMap.put(APP_NAME_18, R.drawable.new_office_my_wiped_icon);
        appIconIdMap.put(APP_NAME_19, R.drawable.new_office_my_account_changes_icon);
        // 人事
        appIconIdMap.put(APP_NAME_20, R.drawable.new_office_my_need_hand_icon);
        appIconIdMap.put(APP_NAME_21, R.drawable.new_office_my_talent_recommendation_icon);
        appIconIdMap.put(APP_NAME_22, R.drawable.new_office_my_interview_arrangements_icon);
        appIconIdMap.put(APP_NAME_23, R.drawable.new_office_my_interview_feedback_icon);
        appIconIdMap.put(APP_NAME_24, R.drawable.new_office_my_positive_staff_icon);
        appIconIdMap.put(APP_NAME_25, R.drawable.new_office_my_transfer_position_icon);
        appIconIdMap.put(APP_NAME_26, R.drawable.new_office_my_change_salary_icon);
        appIconIdMap.put(APP_NAME_27, R.drawable.new_office_my_resignation_application_icon);
        appIconIdMap.put(APP_NAME_28, R.drawable.new_office_my_contract_renewal_icon);
        appIconIdMap.put(APP_NAME_29, R.drawable.new_office_my_books_icon);
        // 行政
        appIconIdMap.put(APP_NAME_30, R.drawable.new_make_administrative_notify_icon);
        appIconIdMap.put(APP_NAME_31, R.drawable.new_make_administrative_meeting_icon);
        appIconIdMap.put(APP_NAME_32, R.drawable.new_make_official_seal_manager_icon);
        appIconIdMap.put(APP_NAME_33, R.drawable.new_make_administrative_official_seal);
        appIconIdMap.put(APP_NAME_34, R.drawable.new_make_administrative_my_car_icon);
        appIconIdMap.put(APP_NAME_35, R.drawable.new_make_administrative_about_car_icon);
        // 销售
        appIconIdMap.put(APP_NAME_36, R.drawable.new_sell_clue_icon);
        appIconIdMap.put(APP_NAME_37, R.drawable.new_sell_clue_manage_icon);
        appIconIdMap.put(APP_NAME_38, R.drawable.new_sell_chance_icon);
        appIconIdMap.put(APP_NAME_39, R.drawable.new_sell_customer_info_icon);
        appIconIdMap.put(APP_NAME_40, R.drawable.new_sell_contacts_icon);
        appIconIdMap.put(APP_NAME_41, R.drawable.new_sell_share_customer_icon);
        appIconIdMap.put(APP_NAME_42, R.drawable.new_sell_invoice_icon);
        appIconIdMap.put(APP_NAME_43, R.drawable.new_sell_agreement_icon);
        appIconIdMap.put(APP_NAME_44, R.drawable.new_sell_share_agreement_icon);
        // 商务财务
        appIconIdMap.put(APP_NAME_45, R.drawable.new_sell_billing_icon);
        appIconIdMap.put(APP_NAME_46, R.drawable.new_sell_refunds_icon);
        appIconIdMap.put(APP_NAME_47, R.drawable.new_sell_collection_verification_icon);
        // 远程
        appIconIdMap.put(APP_NAME_48, R.drawable.new_make_monitored_icon);
        appIconIdMap.put(APP_NAME_49, R.drawable.new_make_decryption_icon);
        appIconIdMap.put(APP_NAME_50, R.drawable.new_make_outgoing_icon);
        // 项目
        appIconIdMap.put(APP_NAME_51, R.drawable.new_make_project_icon);
        appIconIdMap.put(APP_NAME_52, R.drawable.new_make_schedule_icon);
        appIconIdMap.put(APP_NAME_53, R.drawable.new_make_task_icon);
        // 供应商
        appIconIdMap.put(APP_NAME_54, R.drawable.new_make_supplier_company_icon);
        appIconIdMap.put(APP_NAME_55, R.drawable.new_make_payment_icon);
        appIconIdMap.put(APP_NAME_56, R.drawable.new_make_wiped_icon);
        appIconIdMap.put(APP_NAME_57, R.drawable.new_make_business_icon);
        appIconIdMap.put(APP_NAME_58, R.drawable.purchase_requisition_icon);
        appIconIdMap.put(APP_NAME_59, R.drawable.procurement_execution_feedback_icon);
        appIconIdMap.put(APP_NAME_60, R.drawable.warehousing_appliaction_icon);
        // 资产
        appIconIdMap.put(APP_NAME_61, R.drawable.new_assets_office_supplies_collar_icon);
        appIconIdMap.put(APP_NAME_62, R.drawable.new_assets_collar_management_icon);
        appIconIdMap.put(APP_NAME_63, R.drawable.new_assets_my_demand_icon);
        appIconIdMap.put(APP_NAME_64, R.drawable.new_assets_my_fixed_icon);
        appIconIdMap.put(APP_NAME_65, R.drawable.new_assets_fixed_loan_icon);
        appIconIdMap.put(APP_NAME_66, R.drawable.new_assets_return_of_fixed_icon);
        appIconIdMap.put(APP_NAME_67, R.drawable.new_assets_warehousint_confirmation_icon);
    }

    /**
     * 我的应用
     * 根据appName动态获取app的iconId
     * @param appName
     * @return
     */
    public static Integer getAppIconId(String appName){
        if (appIconIdMap.containsKey(appName)){
            return appIconIdMap.get(appName);
        }
        return 0;
    }


最后通过getAppIconId(appName)方法获取到id,这样动态添加就解决了这个问题了,

其实,我发现我们的R文件(自动生成),每次启动的资源id可能都会变化,资源id的int值并不是一成不变的,所以每次都静态统计一次,然后通过对应的key来取对应的drawable的resource id。这样动态解决了这个问题了。哈哈哈~    

如果大家有什么问题,欢迎一起交流~~~


你可能感兴趣的:(android)