如何为jetpack组成的列表创建一个可变状态?

问题描述 投票:0回答:1
如何正确组合列表中的列表可变状态 我遇到了有关“无法访问kotlin.collections.mutablist的问题”

该行中的错误

inputs = inputs + name

@Composable
fun CreateBar(){
    var name by remember { mutableStateOf("") }
    val inputs = remember { mutableStateListOf<String>() }
    Column {
        Row( modifier = Modifier
            .fillMaxWidth()
            .padding(20.dp , 60.dp),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically)
        {
            TextField(
                value = name,
                onValueChange = { text : String ->
                    name = text
                }
            )
            Button(
                onClick = {
                        inputs = inputs + name
                }
            ) {
                Icon(
                    imageVector = Icons.Default.Search,
                    contentDescription = null,
                    modifier = Modifier
                        .height(45.dp)
                )
            }
        }
        LazyColumn {
            items(inputs){ input ->
                Text("this is"+input)
            }
        }
    }

}
	
android list kotlin android-jetpack-compose state
1个回答
0
投票

检查MutableStateList的正确导入 - >
  1. import androidx.compose.runtime.mutableStateListOf
    检查Gradle的正确依赖性。即使您使用的是Android Studio的较新版本,也不会有问题。
  1. 清除并再次重建项目。
  2. 如果问题持续存在,那么您可以使用此

  3. var inputsMut by remember { mutableStateOf<List<String>>(emptyList()) } inputsMut = inputsMut + name //Insert the name on button click

  4. **注意:**请记住,在幕后“mutableStateof
()”和“ mutableStateListof”的工作方式不同。但是在您的情况下,它将完成工作。

在现有代码中,您正在以错误的方式更新“ mutableStateListof”。 MutableStateList已经可以变形,因此您无需重新分配它,只需使用“ .ADD(name)”函数对其进行修改。

    父母布局不使用“列”,而只需使用“ lazycolumn”,否则可能会创建一些未知的UI相关问题。
  1. 这是我方面的最终修改代码=>
  2. @Composable fun CreateBar( modifier: Modifier = Modifier ){ var name by remember { mutableStateOf("") } val inputs = remember { mutableStateListOf<String>() } var inputsMut by remember { mutableStateOf<List<String>>(emptyList()) } //Use lazy column as it will minimizes recomposition and also good for this type of scenarios LazyColumn ( modifier = modifier ){ item { Row( modifier = Modifier .fillMaxWidth() .padding(20.dp , 60.dp), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) { TextField( value = name, onValueChange = { text : String -> name = text } ) Button( onClick = { inputs.add(name) // Use this to modify the input list // inputsMut = inputsMut + name // Use this to modify the input list, if you are using mutableStateOf name = "" // After adding change the name to empty } ) { androidx.compose.material3.Icon( imageVector = Icons.Default.Search, contentDescription = null, modifier = Modifier .height(45.dp) ) } } } //Use this to show the input list, if you are using mutableStateOf // items(inputsMut){ input -> // Text("this is$input") // } items(inputs){ input -> Text("this is$input") } } }

    我希望我的答案对您有帮助。随时提出与之相关的新问题,或者就此问题进行任何进一步的澄清。 谢谢

© www.soinside.com 2019 - 2025. All rights reserved.