LazyColumn不显示。

huangapple go评论96阅读模式
英文:

LazyColumn dont displaying

问题

以下是代码部分的中文翻译:

  1. class DiscoverActivity : ComponentActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContent {
  5. FluctusTheme {
  6. val navController = rememberNavController()
  7. DiscoverActivityUI(navController)
  8. }
  9. }
  10. }
  11. }
  12. @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
  13. @Composable
  14. fun DiscoverActivityUI(navController: NavController?) {
  15. val systemUiController = rememberSystemUiController()
  16. DisposableEffect(systemUiController) {
  17. systemUiController.setStatusBarColor(
  18. color = backgroundColor,
  19. )
  20. systemUiController.setNavigationBarColor(
  21. color = white
  22. )
  23. onDispose {}
  24. }
  25. Scaffold(
  26. modifier = Modifier.fillMaxSize(),
  27. bottomBar = { BottomNavigationBar(navController = navController) }
  28. ) {
  29. Surface(
  30. color = backgroundColor,
  31. modifier = Modifier.fillMaxSize()
  32. ) {
  33. BotList()
  34. }
  35. }
  36. }
  37. @Composable
  38. fun BotList() {
  39. val botData = BotData()
  40. LaunchedEffect(Unit) {
  41. botData.mutableBotList.clear()
  42. botData.updateBotList()
  43. }
  44. LazyColumn(
  45. modifier = Modifier.fillMaxWidth()
  46. ) {
  47. items(botData.mutableBotList) { bot ->
  48. bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
  49. bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
  50. bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }
  51. }
  52. }
  53. }
  54. class BotData {
  55. var fireStoreInstance = FirebaseFirestore.getInstance()
  56. data class Bots(
  57. val name: String?,
  58. val type: String?,
  59. val profit_percent: String?,
  60. val isOnCurrentAccount: Boolean?
  61. )
  62. fun updateBotList() {
  63. fireStoreInstance.collection("marketplace").get().addOnCompleteListener {
  64. if(it.isSuccessful) {
  65. for(document in it.result) {
  66. val name = document.getString("name")
  67. val type = document.getString("type")
  68. val profit_percent = document.getString("profit_percent")
  69. val newBot = Bots(
  70. name = name,
  71. type = type,
  72. profit_percent = profit_percent,
  73. isOnCurrentAccount = false
  74. )
  75. mutableBotList.add(newBot)
  76. println(mutableBotList)
  77. }
  78. } else {
  79. println("hubo un error")
  80. }
  81. }
  82. }
  83. val mutableBotList = mutableListOf<Bots>()
  84. }

请注意,代码中的文本未做任何更改。如果您有关于问题的更多信息或需要进一步的帮助,请随时提出。

英文:

I want to make a list of bots that I get from my database in firebase, but I don't know why the text() of BotList() are not shown on the screen, although the implementation seems correct, thank you very much.

  1. class DiscoverActivity : ComponentActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContent {
  5. FluctusTheme {
  6. val navController = rememberNavController()
  7. DiscoverActivityUI(navController)
  8. }
  9. }
  10. }
  11. }
  12. @SuppressLint(&quot;UnusedMaterialScaffoldPaddingParameter&quot;)
  13. @Composable
  14. fun DiscoverActivityUI(navController: NavController?) {
  15. val systemUiController = rememberSystemUiController()
  16. DisposableEffect(systemUiController) {
  17. systemUiController.setStatusBarColor(
  18. color = backgroundColor,
  19. )
  20. systemUiController.setNavigationBarColor(
  21. color = white
  22. )
  23. onDispose {}
  24. }
  25. Scaffold(
  26. modifier = Modifier.fillMaxSize(),
  27. bottomBar = { BottomNavigationBar(navController = navController) }
  28. ) {
  29. Surface(
  30. color = backgroundColor,
  31. modifier = Modifier.fillMaxSize()
  32. ) {
  33. ** BotList() **
  34. }
  35. }
  36. }
  37. @Composable
  38. fun BotList() {
  39. val botData = BotData()
  40. LaunchedEffect(Unit) {
  41. botData.mutableBotList.clear()
  42. botData.updateBotList()
  43. }
  44. LazyColumn(
  45. modifier = Modifier.fillMaxWidth()
  46. ) {
  47. **items(botData.mutableBotList) { bot -&gt;
  48. bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
  49. bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
  50. bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }**
  51. }
  52. }
  53. }
  1. class BotData {
  2. var fireStoreInstance = FirebaseFirestore.getInstance()
  3. data class Bots(
  4. val name: String?,
  5. val type: String?,
  6. val profit_percent: String?,
  7. val isOnCurrentAccount: Boolean?
  8. )
  9. fun updateBotList() {
  10. fireStoreInstance.collection(&quot;marketplace&quot;).get().addOnCompleteListener {
  11. if(it.isSuccessful) {
  12. for(document in it.result) {
  13. val name = document.getString(&quot;name&quot;)
  14. val type = document.getString(&quot;type&quot;)
  15. val profit_percent = document.getString(&quot;profit_percent&quot;)
  16. val newBot = Bots(
  17. name = name,
  18. type = type,
  19. profit_percent = profit_percent,
  20. isOnCurrentAccount = false
  21. )
  22. mutableBotList.add(newBot)
  23. println(mutableBotList)
  24. }
  25. } else {
  26. println(&quot;hubo un error&quot;)
  27. }
  28. }
  29. }
  30. val mutableBotList = mutableListOf&lt;Bots&gt;()
  31. }

texts in the lazy columns dont showing in the screen, although the implementation seems correct, thank you very much.

答案1

得分: 0

首先为你的机器人数据添加一个ViewModel。

  1. class BotViewModel: ViewModel() {
  2. var fireStoreInstance = FirebaseFirestore.getInstance()
  3. data class Bots(
  4. val name: String?,
  5. val type: String?,
  6. val profit_percent: String?,
  7. val isOnCurrentAccount: Boolean?
  8. )
  9. fun updateBotList() {
  10. val mutableBotList = mutableListOf<Bots>()
  11. fireStoreInstance.collection("marketplace").get().addOnCompleteListener {
  12. if(it.isSuccessful) {
  13. for(document in it.result) {
  14. val name = document.getString("name")
  15. val type = document.getString("type")
  16. val profit_percent = document.getString("profit_percent")
  17. val newBot = Bots(
  18. name = name,
  19. type = type,
  20. profit_percent = profit_percent,
  21. isOnCurrentAccount = false
  22. )
  23. mutableBotList.add(newBot)
  24. println(mutableBotList)
  25. }
  26. mutableBotLiveData.value = mutableBotList
  27. } else {
  28. println("hubo un error")
  29. }
  30. }
  31. }
  32. val mutableBotLiveData = MutableLiveData<MutableList<Bots>>()
  33. }

接下来将ViewModel添加到你的Activity。

  1. class DiscoverActivity : ComponentActivity() {
  2. val botViewModel by viewModels<BotViewModel>()
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContent {
  6. FluctusTheme {
  7. val navController = rememberNavController()
  8. DiscoverActivityUI(navController, botViewModel)
  9. }
  10. }
  11. }
  12. }

将ViewModel传递给你的BotsList组件。

  1. @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
  2. @Composable
  3. fun DiscoverActivityUI(navController: NavController?, viewModel: BotViewModel) {
  4. val systemUiController = rememberSystemUiController()
  5. DisposableEffect(systemUiController) {
  6. systemUiController.setStatusBarColor(
  7. color = backgroundColor,
  8. )
  9. systemUiController.setNavigationBarColor(
  10. color = white
  11. )
  12. onDispose {}
  13. }
  14. Scaffold(
  15. modifier = Modifier.fillMaxSize(),
  16. bottomBar = { BottomNavigationBar(navController = navController) }
  17. ) {
  18. Surface(
  19. color = backgroundColor,
  20. modifier = Modifier.fillMaxSize()
  21. ) {
  22. BotList(
  23. viewModel = viewModel
  24. )
  25. }
  26. }
  27. }

最后,使用observeAsState与你的ViewModel数据,当数据发生变化时,该组件将重新组合,即在updateBotList中调用mutableBotLiveData.value = mutableBotList时。

  1. @Composable
  2. fun BotList(viewModel: BotViewModel) {
  3. val botsData by viewModel.mutableBotLiveData.observeAsState(mutableListOf())
  4. LaunchedEffect(Unit) {
  5. viewModel.mutableBotLiveData.value!!.clear()
  6. viewModel.updateBotList()
  7. }
  8. LazyColumn(
  9. modifier = Modifier.fillMaxWidth()
  10. ) {
  11. items(botsData) { bot ->
  12. bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
  13. bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
  14. bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }
  15. }
  16. }
  17. }
英文:

First add a ViewModel for your Bots data.

  1. class BotViewModel: ViewModel() {
  2. var fireStoreInstance = FirebaseFirestore.getInstance()
  3. data class Bots(
  4. val name: String?,
  5. val type: String?,
  6. val profit_percent: String?,
  7. val isOnCurrentAccount: Boolean?
  8. )
  9. fun updateBotList() {
  10. val mutableBotList = mutableListOf&lt;Bots&gt;()
  11. fireStoreInstance.collection(&quot;marketplace&quot;).get().addOnCompleteListener {
  12. if(it.isSuccessful) {
  13. for(document in it.result) {
  14. val name = document.getString(&quot;name&quot;)
  15. val type = document.getString(&quot;type&quot;)
  16. val profit_percent = document.getString(&quot;profit_percent&quot;)
  17. val newBot = Bots(
  18. name = name,
  19. type = type,
  20. profit_percent = profit_percent,
  21. isOnCurrentAccount = false
  22. )
  23. mutableBotList.add(newBot)
  24. println(mutableBotList)
  25. }
  26. mutableBotLiveData.value = mutableBotList
  27. } else {
  28. println(&quot;hubo un error&quot;)
  29. }
  30. }
  31. }
  32. val mutableBotLiveData = MutableLiveData&lt;MutableList&lt;Bots&gt;&gt;()
  33. }

Next add the View Model to your Activity.

  1. class DiscoverActivity : ComponentActivity() {
  2. val botViewModel by viewModels&lt;BotViewModel&gt;()
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContent {
  6. FluctusTheme {
  7. val navController = rememberNavController()
  8. DiscoverActivityUI(navController, botViewModel)
  9. }
  10. }
  11. }
  12. }

Pass the ViewModel through to your BotsList composable.

  1. @SuppressLint(&quot;UnusedMaterialScaffoldPaddingParameter&quot;)
  2. @Composable
  3. fun DiscoverActivityUI(navController: NavController?, viewModel: BotViewModel) {
  4. val systemUiController = rememberSystemUiController()
  5. DisposableEffect(systemUiController) {
  6. systemUiController.setStatusBarColor(
  7. color = backgroundColor,
  8. )
  9. systemUiController.setNavigationBarColor(
  10. color = white
  11. )
  12. onDispose {}
  13. }
  14. Scaffold(
  15. modifier = Modifier.fillMaxSize(),
  16. bottomBar = { BottomNavigationBar(navController = navController) }
  17. ) {
  18. Surface(
  19. color = backgroundColor,
  20. modifier = Modifier.fillMaxSize()
  21. ) {
  22. BotList(
  23. viewModel = viewModel
  24. )
  25. }
  26. }
  27. }

Finally use observeAsState with your ViewModel data and the composable will recompose when the data changes, in this case when mutableBotLiveData.value = mutableBotList is called in updateBotList.

  1. @Composable
  2. fun BotList(viewModel: BotViewModel) {
  3. val botsData by viewModel.mutableBotLiveData.observeAsState(mutableListOf())
  4. LaunchedEffect(Unit) {
  5. viewModel.mutableBotLiveData.value!!.clear()
  6. viewModel.updateBotList()
  7. }
  8. LazyColumn(
  9. modifier = Modifier.fillMaxWidth()
  10. ) {
  11. items(botsData) { bot -&gt;
  12. bot.name?.let { Text(text = it, style = TextStyle(fontSize = 16.sp)) }
  13. bot.type?.let { Text(text = it, style = TextStyle(fontSize = 14.sp)) }
  14. bot.profit_percent?.let { Text(text = it, style = TextStyle(fontSize = 12.sp)) }
  15. }
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年7月28日 02:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定