如何使用Retrofit调用API后筛选列表中的IPL赛事
客户端筛选IPL赛事的实现方案
因为API本身不支持筛选功能,你可以在拿到Retrofit返回的完整赛事列表后,在客户端做过滤处理,具体步骤如下:
步骤1:获取目标series_id
从API返回的第4、5项赛事数据中,提取它们共同的series_id值,这个值就是IPL赛事的唯一标识。步骤2:过滤赛事列表
遍历所有赛事,保留series_id与目标值匹配的项,再用过滤后的列表展示UI。
代码示例
Kotlin版本
// 假设你的赛事数据类为Match,包含series_id字段 val allMatches = apiResponse.matches // 从Retrofit响应中获取全部赛事 val targetIplSeriesId = "你的IPL赛事series_id" // 替换为实际获取的ID val iplMatches = allMatches.filter { it.series_id == targetIplSeriesId } // 后续使用iplMatches渲染赛事列表
Java版本
// 假设Match类提供getSeriesId()方法获取赛事系列ID List<Match> allMatches = apiResponse.getMatches(); String targetIplSeriesId = "你的IPL赛事series_id"; // 替换为实际获取的ID List<Match> iplMatches = allMatches.stream() .filter(match -> targetIplSeriesId.equals(match.getSeriesId())) .collect(Collectors.toList()); // 用iplMatches更新UI展示
备选方案(无需硬编码series_id)
如果不想固定series_id,也可以通过赛事名称关键词筛选(比如匹配"IPL"或"Indian Premier League"):
val iplMatches = allMatches.filter { it.series_name.contains("IPL", ignoreCase = true) || it.series_name.contains("Indian Premier League", ignoreCase = true) }
内容的提问来源于stack exchange,提问作者Arpit Gupta
相关产品推荐
相关产品推荐

