结合流< list>和kotlin中的字符串
嘿,我在Kotlin Flow工作。我有来自服务器的数据列表的流程。我想过滤文本,我尝试了一些代码,但这给了我问题。有人可以指导我吗?谢谢
ExploreViewModel.kt
class ExploreViewModel : BaseViewModel() {
private val query = MutableStateFlow("")
var queryText: String
get() = query.value
set(value) {
query.value = value
}
val filteredTopics = MutableStateFlow<List<ConsultationTopics>>(emptyList())
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(filteredTopics) { queryText, categoriesList ->
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
return@combine filteredTopics
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
}
}
我有filteredTopics
属性,这些数据来自服务器,queryText
使用onqueryTextChange
来自<代码>销售范围。我正在尝试过滤数据并创建名为filteredCategories
的新属性,以将此值传递给适配器。我正在尝试filteredCategories
我正在检查queryText
是空的,然后将整个列表传递,否则仅通过过滤器列表,但是我不知道这是正确的方法。
错误
private fun setupFilteredTopic() {
lifecycleScope.launchWhenCreated {
repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.filteredCategories.collect { filteredTopicsList ->
consultationAdapter.submitList(filteredTopicsList)
}
}
}
}
有人可以指导我。谢谢
Hey I am working in kotlin flow. I have flow in which I have list of data coming from server. And I want to filter text, I tried some piece of code but it's giving me problem. Can someone guide me on this. Thanks
ExploreViewModel.kt
class ExploreViewModel : BaseViewModel() {
private val query = MutableStateFlow("")
var queryText: String
get() = query.value
set(value) {
query.value = value
}
val filteredTopics = MutableStateFlow<List<ConsultationTopics>>(emptyList())
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(filteredTopics) { queryText, categoriesList ->
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
return@combine filteredTopics
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
}
}
I have filteredTopics
property which have all data coming from server, queryText
is using onQueryTextChange
from serarchview
. I am trying to filter the data and create new property called filteredCategories
to pass this value to adapter. I am trying in filteredCategories
I am checking queryText
is empty then passing whole list otherwise pass filter list only, but I don't know this is correct way of doing or not.
Error
private fun setupFilteredTopic() {
lifecycleScope.launchWhenCreated {
repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.filteredCategories.collect { filteredTopicsList ->
consultationAdapter.submitList(filteredTopicsList)
}
}
}
}
Can someone guide me. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于,您返回了过滤类型的不同类型的元素
,因此您需要对代码进行一些更改,更改
return@combine filteredTopics
byrether> return@combine@combine filteredtopics.value
:这应该解决错误
The problem is that you returning elements of different types for filteredCategories
so you need to make a little change to your code, change
return@combine filteredTopics
byreturn@combine filteredTopics.value
:this should fix the error
您应该返回
ConsultationTopics
中的对象
combine 使用categoriesList
而不是filteredtopics
:或我们可以忽略我们
return@combine
操作员:You should return a list of
ConsultationTopics
objects in thecombine
block usingcategoriesList
instead offilteredTopics
:Or we can omit the
return@combine
operator: