android LazyColumn不显示

ehxuflar  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(131)

我想做一个列表的机器人,我从我的数据库在firebase,但我不知道为什么文本()的BotList()不显示在屏幕上,虽然实现似乎是正确的,非常感谢.

class DiscoverActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            FluctusTheme {
                val navController = rememberNavController()
                DiscoverActivityUI(navController)
            }
        }
    }
}

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun DiscoverActivityUI(navController: NavController?) {
    val systemUiController = rememberSystemUiController()
    DisposableEffect(systemUiController) {
        systemUiController.setStatusBarColor(
            color = backgroundColor,
        )
        systemUiController.setNavigationBarColor(
            color = white
        )
        onDispose {}
    }

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = { BottomNavigationBar(navController = navController) }
    )  {
        Surface(
            color = backgroundColor,
            modifier = Modifier.fillMaxSize()
        ) {
           ** BotList() **
        }
    }
}


@Composable
fun BotList() {
    val botData = BotData()

    LaunchedEffect(Unit) {
        botData.mutableBotList.clear()
        botData.updateBotList()
    }

    LazyColumn(
        modifier = Modifier.fillMaxWidth()
    ) {
        **items(botData.mutableBotList) { bot ->
            bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
            bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
            bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }**
        }
    }
}

个字符
懒惰列中的文本不会显示在屏幕上,尽管实现似乎是正确的,非常感谢。

ss2ws0br

ss2ws0br1#

首先为你的Bots数据添加ViewModel。

class BotViewModel: ViewModel() {

    var fireStoreInstance = FirebaseFirestore.getInstance()

    data class Bots(
        val name: String?,
        val type: String?,
        val profit_percent: String?,
        val isOnCurrentAccount: Boolean?
    )

    fun updateBotList() {
        val mutableBotList = mutableListOf<Bots>()
        fireStoreInstance.collection("marketplace").get().addOnCompleteListener {
            if(it.isSuccessful) {
                for(document in it.result) {
                    val name = document.getString("name")
                    val type = document.getString("type")
                    val profit_percent = document.getString("profit_percent")

                    val newBot = Bots(
                        name = name,
                        type = type,
                        profit_percent = profit_percent,
                        isOnCurrentAccount = false
                    )
                    mutableBotList.add(newBot)
                    println(mutableBotList)
                }

                mutableBotLiveData.value = mutableBotList
            } else {
                println("hubo un error")
            }
        }
    }

    val mutableBotLiveData = MutableLiveData<MutableList<Bots>>()
}

字符串
接下来将视图模型添加到您的活动中。

class DiscoverActivity : ComponentActivity() {
    val botViewModel by viewModels<BotViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            FluctusTheme {
                val navController = rememberNavController()
                DiscoverActivityUI(navController, botViewModel)
            }
        }
    }
}


将ViewModel传递到BotsList可组合。

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun DiscoverActivityUI(navController: NavController?, viewModel: BotViewModel) {
    val systemUiController = rememberSystemUiController()
    DisposableEffect(systemUiController) {
        systemUiController.setStatusBarColor(
            color = backgroundColor,
        )
        systemUiController.setNavigationBarColor(
            color = white
        )
        onDispose {}
    }

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = { BottomNavigationBar(navController = navController) }
    )  {
        Surface(
            color = backgroundColor,
            modifier = Modifier.fillMaxSize()
        ) {
            BotList(
                viewModel = viewModel
            )
        }
    }
}


最后使用observeAsState处理ViewModel数据,当数据发生变化时,组合函数将重新组合,在本例中,当updateBotList中调用mutableBotLiveData.value = mutableBotList

@Composable
fun BotList(viewModel: BotViewModel) {
    val botsData by viewModel.mutableBotLiveData.observeAsState(mutableListOf())

    LaunchedEffect(Unit) {
        viewModel.mutableBotLiveData.value!!.clear()
        viewModel.updateBotList()
    }

    LazyColumn(
        modifier = Modifier.fillMaxWidth()
    ) {
        items(botsData) { bot ->
            bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
            bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
            bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }
        }
    }
}

相关问题