有没有办法在 Kotlin 中进行多重过滤?
Is there a way to do multiple filtering in Kotlin?
我正在为 android 创建一个播客应用程序。我想过滤播客列表,以便获得健康类型 only.However 大约有 3 种不同的健康类型。我决定过滤这三个。现在,每当我 运行 应用程序时,我都会得到一个空列表 showing.However 如果我应该只过滤一种健康类型,一切正常 perfect.Here 是我的代码。
暂停乐趣 getHealthPodcast():列表 {
val requireGenreHF =Genre("1512", "Health & Fitness","https://itunes.apple.com/gb/genre/id1512")
val requireGenreAH =Genre("1513", "Alternative Health","https://itunes.apple.com/gb/genre/id1513")
val requireGenreMH =Genre("1517", "Mental Health","https://itunes.apple.com/gb/genre/id1517")
val listGenre = listOf(requireGenreHF, requireGenreAH, requireGenreMH)
val results = itunesRepo?.getHealthPodcast()
if (results != null && results.isSuccessful) {
val podcasts = results.body()?.feed?.results
val filteredData = podcasts?.filter {
it.genres.containsAll(listGenre)
}
if (filteredData != null) {
return filteredData.map { podcast ->
itunesPodcastView(podcast)
}
}
}
return emptyList()
}
containsAll()
要求genres
包含listGenre
的所有个元素,所以播客必须同时标记为每个健康类型时间。如果您想搜索 任何 健康类型,您可以这样做:
it.genres.any { it in listGenre }
让我们深入研究您的代码。
I am creating a podcast app for android. I want to filter list of podcast so I could get Health genre only.However there are about 3 different Health genres. I decided to filter all three of it. Now anytime I run the app,I get an empty list showing
现在让我们看看这一行
val filteredData = podcasts?.filter {
it.genres.containsAll(listGenre)
}
如果你深入研究 containsAll
方法的 documentation。这是定义的
Checks if all elements in the specified collection are contained in this collection.
所以问题是它确保过滤具有所有指定类型的元素,这些类型可能不存在,因此您得到一个空列表。
现在得出答案和解决方案,您需要的只是或条件
val filteredData = podcasts?.filter {
it.genres.contains(requireGenreHF) || it.genres.contains(requireGenreAH) || it.genres.contains(requireGenreH)
}
像any
调用这样的过滤方法还有很多,你可以研究一下。他们更加精致。
我正在为 android 创建一个播客应用程序。我想过滤播客列表,以便获得健康类型 only.However 大约有 3 种不同的健康类型。我决定过滤这三个。现在,每当我 运行 应用程序时,我都会得到一个空列表 showing.However 如果我应该只过滤一种健康类型,一切正常 perfect.Here 是我的代码。
暂停乐趣 getHealthPodcast():列表 {
val requireGenreHF =Genre("1512", "Health & Fitness","https://itunes.apple.com/gb/genre/id1512")
val requireGenreAH =Genre("1513", "Alternative Health","https://itunes.apple.com/gb/genre/id1513")
val requireGenreMH =Genre("1517", "Mental Health","https://itunes.apple.com/gb/genre/id1517")
val listGenre = listOf(requireGenreHF, requireGenreAH, requireGenreMH)
val results = itunesRepo?.getHealthPodcast()
if (results != null && results.isSuccessful) {
val podcasts = results.body()?.feed?.results
val filteredData = podcasts?.filter {
it.genres.containsAll(listGenre)
}
if (filteredData != null) {
return filteredData.map { podcast ->
itunesPodcastView(podcast)
}
}
}
return emptyList()
}
containsAll()
要求genres
包含listGenre
的所有个元素,所以播客必须同时标记为每个健康类型时间。如果您想搜索 任何 健康类型,您可以这样做:
it.genres.any { it in listGenre }
让我们深入研究您的代码。
I am creating a podcast app for android. I want to filter list of podcast so I could get Health genre only.However there are about 3 different Health genres. I decided to filter all three of it. Now anytime I run the app,I get an empty list showing
现在让我们看看这一行
val filteredData = podcasts?.filter {
it.genres.containsAll(listGenre)
}
如果你深入研究 containsAll
方法的 documentation。这是定义的
Checks if all elements in the specified collection are contained in this collection.
所以问题是它确保过滤具有所有指定类型的元素,这些类型可能不存在,因此您得到一个空列表。
现在得出答案和解决方案,您需要的只是或条件
val filteredData = podcasts?.filter {
it.genres.contains(requireGenreHF) || it.genres.contains(requireGenreAH) || it.genres.contains(requireGenreH)
}
像any
调用这样的过滤方法还有很多,你可以研究一下。他们更加精致。