You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中关联ArrayList<String>与图标及运动名取图标的最佳实践

优化运动图标匹配方案 & ArrayList关联最佳实践

嘿,针对你在Android里处理50个运动图标的需求,我来分享几个更高效的方案,比冗长的switch-case清爽多了~

一、替换Switch-Case:用HashMap做映射

你现在的switch写法在图标数量多的时候会变得非常臃肿,维护起来也麻烦(新增/删除运动都要改一堆case)。用**HashMap<String, Integer>**来存储运动名称和对应图标资源ID的映射,会简洁很多,而且查找效率更高。

实现步骤:

  1. 初始化一个静态的HashMap,提前把所有运动-图标对应关系存好
  2. 在getSportIcon方法里直接从Map中取值,同时处理空值和未匹配到的情况

代码示例:

public class SportIconUtils {
    // 提前初始化运动-图标映射
    private static final Map<String, Integer> SPORT_ICON_MAP = new HashMap<>();

    static {
        // 批量添加所有运动和对应的图标ID
        SPORT_ICON_MAP.put("Swimming", R.drawable.swimming);
        SPORT_ICON_MAP.put("Bicycling", R.drawable.bicycling);
        SPORT_ICON_MAP.put("Football", R.drawable.football);
        SPORT_ICON_MAP.put("Badminton", R.drawable.badminton);
        // ... 剩下的46个运动依次添加
    }

    public static int getSportIcon(String sport) {
        if (sport == null) {
            return R.drawable.default_sport_icon; // 传入null时返回默认图标
        }
        // 可以统一转成小写/大写,避免大小写不匹配的问题
        String normalizedSport = sport.trim();
        return SPORT_ICON_MAP.getOrDefault(normalizedSport, R.drawable.default_sport_icon);
    }
}

额外优化:

如果担心拼写大小写问题(比如用户传入"swimming"小写),可以在存Map的时候把key转成小写,取值时也转成小写:

// 存的时候
SPORT_ICON_MAP.put("Swimming".toLowerCase(), R.drawable.swimming);
// 取的时候
String normalizedSport = sport.trim().toLowerCase();

二、图标与ArrayList关联的最佳实践

如果你的ArrayList是用来存储运动名称列表,需要对应每个名称的图标,这里有两种推荐的做法:

方案1:直接遍历ArrayList,用上面的HashMap批量获取图标

如果已经有了ArrayList<String> sportNames,可以直接遍历列表,调用getSportIcon获取对应图标:

ArrayList<String> sportNames = new ArrayList<>();
sportNames.add("Swimming");
sportNames.add("Football");

// 批量获取图标ID列表
ArrayList<Integer> sportIcons = new ArrayList<>();
for (String sport : sportNames) {
    sportIcons.add(SportIconUtils.getSportIcon(sport));
}

方案2:创建数据类,直接关联名称和图标(更推荐)

这种方式可以避免字符串匹配的潜在错误(比如拼写错误、大小写问题),代码可读性和安全性更高。

步骤:

  1. 创建一个SportItem数据类,包含运动名称和图标资源ID
  2. 直接使用ArrayList<SportItem>来存储所有运动信息,后续使用时直接获取图标即可

代码示例:

// 数据类
public class SportItem {
    private String name;
    private int iconResId;

    public SportItem(String name, int iconResId) {
        this.name = name;
        this.iconResId = iconResId;
    }

    // Getter方法
    public String getName() { return name; }
    public int getIconResId() { return iconResId; }
}

// 初始化列表
ArrayList<SportItem> sportList = new ArrayList<>();
sportList.add(new SportItem("Swimming", R.drawable.swimming));
sportList.add(new SportItem("Bicycling", R.drawable.bicycling));
// ... 添加剩下的运动

// 使用时直接获取图标
for (SportItem item : sportList) {
    int iconId = item.getIconResId();
    // 用这个ID设置ImageView的图标
}

为什么推荐方案2?

  • 避免了字符串匹配的各种问题(比如大小写、拼写错误)
  • 代码语义更清晰,直接从对象中拿图标,不用再做额外的查找
  • 后续如果要扩展运动的其他属性(比如运动类型、描述),直接在SportItem里加字段就行,扩展性更强

内容的提问来源于stack exchange,提问作者Jan Maloušek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:15:24