【问题标题】:Jetpack compose lazy column swapping a different list not updatingJetpack 撰写惰性列交换不同的列表而不更新
【发布时间】:2022-10-12 22:28:37
【问题描述】:

我是jetpack compose的新手,

我正在向 Lazycolumn 展示一个运行良好的数据集。 当我尝试过滤 o.r.用不同的数据集替换原始数据集,我的 Lazycolumn 最初将其显示为替换的,但在一瞬间它又回到了原始数据集。

这里是我所做的一些 sn-ps,我怀疑我的撰写逻辑并且无法找出

// The main composeable where I am observing the changes and calling ShowList to populate

@Composeable
fun SomeScreen(viewModel : TestviewModel){
    val stateCountryCodeMap = remember { mutableStateOf(mapOf<String?, List<CountryInfo>>()) }
    // observe and retrieve the dataset.
    testViewModel.stateMapCountryInfo.collectAsState().value.let {
        stateCountryCodeMap.value = it
    }

     // Some Test to buttom to load a different data set
     someRandomeButtom.click{
       viewModel. filterCountryList()
       }

     // request to load original data set
     testViewModel.fetchCountryList()

     
     ShowList(
            currentSelected = stateCountryCodeSelectedIndex.value,
            groupedCountryInfo = stateCountryCodeMap.value,
            testViewModel = testViewModel
        )
}

// The ShowList function to display

@Composable
private fun ShowList(
    currentSelected: Pair<String, Int>,
    groupedCountryInfo: Map<String?, List<CountryInfo>>,
    testViewModel: TestViewModel
) {
   // LazyColumn stuff to render itmems from map dataset
}

// and TestviewModel

    val stateMapCountryInfo = MutableStateFlow(mapOf<String?, List<CountryInfo>>())
    val stateSortedCountryInfo = MutableStateFlow(listOf<CountryInfo>())
    fun fetchCountryList() {
               // some IO operation which gives result
                when (val result = getCountryListRepo.invoke()) {
                    is Result.Success -> {
                        val countryInfoResultList = result.data
                        // sort the list by country name and store
                        stateSortedCountryInfo.value  = countryInfoResultList.sortedBy { it.countryName }
                        // save it to map
                        stateMapCountryInfo.value = stateSortedCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }
                   
        }
    }




val stateFilteredCountryInfo = MutableStateFlow(listOf<CountryInfo>())

fun filterCountryList() {
             // some IO operation 
            // filter on the sorted array, // results 2 items -  India, Indonesia
            val filteredList = stateSortedCountryInfo.value.filter {
                it.countryName?.contains("Ind") == true  
            }
            
            // store the filtered result
            stateFilteredCountryInfo.value = filteredList
     
     
           // now assing it to map
            stateMapCountryInfo.value  = stateFilteredCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }

        }
    }
}

到目前为止,它是 ShowList 方法中项目的直接显示。

现在,回到 SomeScreenMethod(..),

现在如果我点击那个随机按钮,这给了我一个不同/过滤列表正如预期的那样 和 LazyColumn 更新它,但又回到原始状态。

你能指出哪里出错了吗?

【问题讨论】:

    标签: android android-jetpack-compose lazycolumn


    【解决方案1】:

    似乎当重组发生时,被触发多次。

    testViewModel.fetchCountryList()

    你能试试这个检查一下吗

     LaunchedEffect(Unit) {
            testViewModel.fetchCountryList()
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多