英文:
Is not mutableStateOf working with arrays to refresh values as well?
问题
I am using mutableStateOf to refresh values on screen but it not working with arrays though it compiler (Android Studio) excepts arrays version of it.
我正在使用mutableStateOf
来刷新屏幕上的值,但它在处理数组时不起作用,尽管编译器(Android Studio)支持数组版本。
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var stringValue by remember { mutableStateOf(stringValues0) }
var oneValue by remember { mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for (k in stringValue.indices) {
Text(text = stringValue[k])
}
}
}
}
In this code, if oneValue had been changed we can see it on screen. But if stringValues0's one item or whole of it changed it is not shown on screen.
在这段代码中,如果oneValue
已更改,我们可以在屏幕上看到它。但如果stringValues0
的一个项或整个数组发生了更改,它不会显示在屏幕上。
This feels pretty idiotic there must be something I am missing.
这感觉相当愚蠢,一定有我漏掉的东西。
英文:
I am using mutableStateOf to refresh values on screen but it not working with arrays though it compiler (Android Studio) excepts arrays version of it.
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var stringValue by remember {mutableStateOf(stringValues0) }
var oneValue by remember {mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for(k in stringValue.indices) {
Text(text = stringValue[k])
}
}
}
}
In this code, if oneValue had been changed we can see it on screen. But if stringValues0's one item or whole of it changed it is not shown on screen.
This feels pretty idiotic there must be something I am missing.
答案1
得分: 1
你可以将你的列表声明为 mutableStateListOf
。这样,你可以执行诸如 add()
和 remove()
等任何操作。
var initialValues = arrayOf("1", "2")
val stringValue = remember { mutableStateListOf<String>() }
// 初始化设置
LaunchedEffect(Unit) {
stringValue.addAll(initialValues)
}
// 显示列表
Row {
for (value in stringValue) {
Text(text = value)
}
}
// 添加元素
Button(
onClick = { stringValue.add("5") }
) {
//...
}
英文:
You can declare your list as mutableStateListOf
. In this way you can do any operations like add()
and remove()
.
var initialValues = arrayOf("1", "2")
val stringValue = remember { mutableStateListOf<String>()}
//Initial setup
LaunchedEffect(Unit){
stringValue.addAll(initialValues)
}
//To display
Row {
for (value in stringValue) {
Text(text = value)
}
}
//To add an element
Button(
onClick = {stringValue.add("5")}
) {
//...
}
答案2
得分: 0
以下是代码的翻译部分:
它是这样工作的,嗯。
当调整stringValues0字符串时,只需更改showThem中的oneArrayItem值。在我的情况下,我使用showThem中的按钮更改数组值。
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var oneArrayItem by remember {mutableStateOf("") }
var oneValue by remember {mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for(k in stringValue0.indices) {
oneArrayItem = stringValues0[k]
Text(text = oneArrayItem)
}
}
//
//
}
希望这对您有帮助。如果您有其他问题,请随时提问。
英文:
It is worked like this, meh.
You just change oneArrayItem value in showThem when stringValues0 string is adjusted. In my case I change array value with button in showThem.
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var oneArrayItem by remember {mutableStateOf("") }
var oneValue by remember {mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for(k in stringValue0.indices) {
oneArrayItem = stringValues0[k]
Text(text = oneArrayItem)
}
}
//
//
}
}
答案3
得分: 0
你应该修改stringValue而不是stringValue0,stringValue是由remember修改的,要记住它的状态。
然后你可以:
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var stringValue by remember { mutableStateOf(stringValues0) }
var oneValue by remember { mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for (k in stringValue.indices) {
Text(text = stringValue[k])
}
}
Button(onClick = {
stringValue[0] = "3"
stringValue = stringValues0.copyOf()
}) {
}
}
}
英文:
You should modify stringValue instead of stringValue0,stringValue is modified by remember to remember its state.
then you can:
var stringValues0 = arrayOf("1", "2")
var oneValue0 = "1"
@Composable
fun showThem() {
var stringValue by remember { mutableStateOf(stringValues0) }
var oneValue by remember { mutableStateOf(oneValue0) }
Column {
Text(text = oneValue)
Row {
for (k in stringValue.indices) {
Text(text = stringValue[k])
}
}
Button(onClick = {
stringValue[0] = "3"
stringValue = stringValues0.copyOf()
}) {
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论