Couldn't get the entire list in Lazy Column Jetpack Compose

106 views Asked by At

I am trying to collect the data and show up in lazy column but only one data is viewed each time how to get collect all but when creating a log in viewModel the entire data is showing but when viewing in compose the only one is showing up how to collect the updated entire list

ViewModel

private val _addingContentDataList = MutableStateFlow<List<GetAllContent>>(emptyList())
val addingContentDataList: StateFlow<List<GetAllContent>> = _addingContentDataList

private val _homeContentData = MutableStateFlow(GetAllContent())
val homeContentData: StateFlow<GetAllContent> get() = _homeContentData


    private fun addingContentsToHomeContentList(mainContent: MainContent){

  if (mainContent.contentType == "Products"){
      _homeContentData.value.contentId = mainContent.contentId
      _homeContentData.value.contentTitle = mainContent.contentTitle
      _homeContentData.value.contentType = mainContent.contentType
      _homeContentData.value.contentPosition = mainContent.contentPosition
      _homeContentData.value.contentTags = mainContent.contentTags
      _homeContentData.value.products = productContentData.value.toMutableList()
      _homeContentData.value.banners = null

  }
    if (mainContent.contentType == "Banners"){
        _homeContentData.value.contentId = mainContent.contentId
        _homeContentData.value.contentTitle = mainContent.contentTitle
        _homeContentData.value.contentType = mainContent.contentType
        _homeContentData.value.contentPosition = mainContent.contentPosition
        _homeContentData.value.contentTags = mainContent.contentTags
        _homeContentData.value.products = null
        _homeContentData.value.banners = bannerContentData.value.toMutableList()

    }

        _addingContentDataList.value = listOf(_homeContentData.value)

    Log.i(TAG,"HomeContentView ${addingContentDataList.value}")
    Log.i(TAG,"HomeContent ${homeContentData.value}")

}

MyLogStatement EntireData

Composable

@Composable
fun SampleHomeScreenUploadSection(
navController: NavController,
viewModel: SampleHomeScreenViewModel = hiltViewModel()
 ){
val homeContentData by viewModel.addingContentDataList.collectAsStateWithLifecycle()

 Scaffold {

    LazyColumn{

        items(items = homeContentData){
            Log.i(TAG,"getting: ${it}")
           if (it.contentType == "Products") {
                Text(text = it.contentTitle!!)
                LazyRow {
                    items(items = it.products!!) {
                        Row(Modifier.clickable { }) {
                            ProductItem(product = it, onProductClick = { })
                        }
                    }
                }
            }
        }
    }
}

Compose Log

  I  getting: GetAllContent(contentId=WrH6c4urrQECUdyb0OOH, contentPosition=2, contentTitle=Second Content, contentType=Products, contentTags=[Rajiv#8, Auckland], products=[ReceiveProduct(product_name=stra

How to collect all and then show up in Lazy List i tried using viewModelScope.Launch but couldn't successfully get can anyone sought out thanks

1

There are 1 answers

4
Jan Itor On

You don't add a value to _addingContentDataList, you replace it with a new list with a single value here:

_addingContentDataList.value = listOf(_homeContentData.value)

Add a value instead

_addingContentDataList.value = _addingContentDataList.value + _homeContentData.value