mutableStateOf是否可以与数组一起使用以刷新数值?

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

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(&quot;1&quot;, &quot;2&quot;)
    val stringValue = remember { mutableStateListOf&lt;String&gt;()}

    //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(&quot;5&quot;)}
    ) {
       //...
    }

答案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(&quot;1&quot;, &quot;2&quot;)
var oneValue0 = &quot;1&quot;

@Composable
fun showThem() { 

  var oneArrayItem by remember {mutableStateOf(&quot;&quot;) }
  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(&quot;1&quot;, &quot;2&quot;)
var oneValue0 = &quot;1&quot;

@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] = &quot;3&quot;
        stringValue = stringValues0.copyOf()
    }) {

    }
  }

}

huangapple
  • 本文由 发表于 2023年3月7日 16:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659343.html
匿名

发表评论

匿名网友

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

确定