结合流< list>和kotlin中的字符串

发布于 2025-02-07 08:09:46 字数 1848 浏览 2 评论 0原文

嘿,我在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)
                }
            }
        }
    }

enter image description here

Can someone guide me. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2025-02-14 08:09:46

问题在于,您返回了过滤类型的不同类型的元素

if (criteria.isEmpty()) {
    return@combine filteredTopics // type MutableStateFlow<List<ConsultationTopics>>
} else {
    categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true } // type List<ConsultationTopics>
}

,因此您需要对代码进行一些更改,更改return@combine filteredTopics by rether> return@combine@combine filteredtopics.value

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()) {
                categoriesList
            } else {
                categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
            }
        }
}

这应该解决错误

The problem is that you returning elements of different types for filteredCategories

if (criteria.isEmpty()) {
    return@combine filteredTopics // type MutableStateFlow<List<ConsultationTopics>>
} else {
    categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true } // type List<ConsultationTopics>
}

so you need to make a little change to your code, change return@combine filteredTopics by return@combine filteredTopics.value:

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()) {
                categoriesList
            } else {
                categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
            }
        }
}

this should fix the error

小姐丶请自重 2025-02-14 08:09:46

您应该返回ConsultationTopics 中的对象 combine 使用categoriesList而不是filteredtopics

val criteria = queryText.lowercase()
return@combine if (criteria.isEmpty()) {
     categoriesList
} else {
     categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}

或我们可以忽略我们return@combine操作员:

val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
     categoriesList
} else {
     categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}

You should return a list of ConsultationTopics objects in the combine block using categoriesList instead of filteredTopics:

val criteria = queryText.lowercase()
return@combine if (criteria.isEmpty()) {
     categoriesList
} else {
     categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}

Or we can omit the return@combine operator:

val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
     categoriesList
} else {
     categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文