获取列表中每个文本字段的值
我有一个列表,其中每个元素包含两个文本字段。一个TextField 是放置键,另一个是放置值。我想获取TextField的值,例如List中key TextField的值和其他List中value TextField的值,或者获取key和value的值并将它们放入MutableMap中(更喜欢最后一个)但我不知道该怎么做。
我该怎么做?我正在使用 LazyColumn 显示 TextField 列表。 现在,我只有两个变量,但所有文本字段的值始终相同,我希望每个文本字段都有不同的值。
val keys = remember { mutableStateOf(TextFieldValue()) }
val values = remember { mutableStateOf(TextFieldValue())}
LazyColumn(Modifier.fillMaxWidth()){
itemsIndexed(myListOfTextFields) { index, item ->
Row(Modifier.fillMaxWidth()) {
TextField(value = keys.value,
onValueChange = { keys.value = it },
singleLine = true)
TextField(value = values.value,
onValueChange = { values.value = it },
singleLine = true)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您希望将列表中的每个输入的“键/值”对存储在KV或映射数组中,
Compose为您提供了函数
mutableStateListOf
允许您在组合范围内与可观察的MutableList
进行交互。Compose 还为您提供了
mutableStateMapOf
但就您而言,我建议您不要使用它,因为您无法轻松地将键/值映射到文本字段中的 index 请参阅代码注释。这是我的建议:
将
List
和Pair
组合成MutableList>
可以让您访问 kotlin 的 libMutableList>.toMap()
将Iterable
转换为Map
由于您的交互依赖于迭代,因此我会强烈坚持使用列表,并稍后在提交数据时将其转换为地图。
data.toMap()
From what I gather your want to store every single inputed "key/value" pair from your list inside an array of KV or and map
Compose provides you with the function
mutableStateListOf
allowing you to interact with an observableMutableList
within the composition scope.Compose also provides you with
mutableStateMapOf
but in your case I'd advise against it since you would not be able to easily map key/value to index within your textfield see code comment.Here is what I'd recommend:
Combining
List
andPair
intoMutableList<Pair<K, V>>
gives you access to kotlin's libMutableList<Pair<K, V>>.toMap()
turningIterable
intoMap<K, V>
Since your interaction relies on an iteration I'd strongly sticking with a list and turning it in a map later on, when submitting data perhaps.
data.toMap()