LazyColumn不显示。

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

LazyColumn dont displaying

问题

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

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)) }
        }
    }
}

class BotData {
    var fireStoreInstance = FirebaseFirestore.getInstance()

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

    fun updateBotList() {
        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)
                }
            } else {
                println("hubo un error")
            }
        }
    }

    val mutableBotList = mutableListOf<Bots>()
}

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

英文:

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.

class DiscoverActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FluctusTheme {
val navController = rememberNavController()
DiscoverActivityUI(navController)
}
}
}
}
@SuppressLint(&quot;UnusedMaterialScaffoldPaddingParameter&quot;)
@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 -&gt;
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)) }**
}
}
}
class BotData {
var fireStoreInstance = FirebaseFirestore.getInstance()
data class Bots(
val name: String?,
val type: String?,
val profit_percent: String?,
val isOnCurrentAccount: Boolean?
)
fun updateBotList() {
fireStoreInstance.collection(&quot;marketplace&quot;).get().addOnCompleteListener {
if(it.isSuccessful) {
for(document in it.result) {
val name = document.getString(&quot;name&quot;)
val type = document.getString(&quot;type&quot;)
val profit_percent = document.getString(&quot;profit_percent&quot;)
val newBot = Bots(
name = name,
type = type,
profit_percent = profit_percent,
isOnCurrentAccount = false
)
mutableBotList.add(newBot)
println(mutableBotList)
}
} else {
println(&quot;hubo un error&quot;)
}
}
}
val mutableBotList = mutableListOf&lt;Bots&gt;()
}

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

答案1

得分: 0

首先为你的机器人数据添加一个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>>()
}

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

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)) }
        }
    }
}
英文:

First add a ViewModel for your Bots data.

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&lt;Bots&gt;()
fireStoreInstance.collection(&quot;marketplace&quot;).get().addOnCompleteListener {
if(it.isSuccessful) {
for(document in it.result) {
val name = document.getString(&quot;name&quot;)
val type = document.getString(&quot;type&quot;)
val profit_percent = document.getString(&quot;profit_percent&quot;)
val newBot = Bots(
name = name,
type = type,
profit_percent = profit_percent,
isOnCurrentAccount = false
)
mutableBotList.add(newBot)
println(mutableBotList)
}
mutableBotLiveData.value = mutableBotList
} else {
println(&quot;hubo un error&quot;)
}
}
}
val mutableBotLiveData = MutableLiveData&lt;MutableList&lt;Bots&gt;&gt;()
}

Next add the View Model to your Activity.

class DiscoverActivity : ComponentActivity() {
val botViewModel by viewModels&lt;BotViewModel&gt;()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FluctusTheme {
val navController = rememberNavController()
DiscoverActivityUI(navController, botViewModel)
}
}
}
}

Pass the ViewModel through to your BotsList composable.

@SuppressLint(&quot;UnusedMaterialScaffoldPaddingParameter&quot;)
@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
)
}
}
}

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.

@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 -&gt;
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)) }
}
}
}

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:

确定