英文:
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("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>()
}
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<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>>()
}
Next add the View Model to your 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)
}
}
}
}
Pass the ViewModel through to your BotsList composable.
@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
)
}
}
}
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 ->
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)) }
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论