获取列表中每个文本字段的值

发布于 2025-01-15 04:49:43 字数 991 浏览 4 评论 0 原文

我有一个列表,其中每个元素包含两个文本字段。一个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)
                   }
      }

I have a List which contains two TextFields for every element. One TextField is to put a key, and the other to put a value. I want to obtain the values of the TextFields, for example, the values of the key TextField in a List and the values of the value TextField in other List, or get the values of the keys and values and put them in a MutableMap(prefer this last one) but I don't know how to do it.

How can I do this? I'm using LazyColumn to show the list of TextField.
Now, I just have two variables but is always the same value for all the TextFields, I want to have a different value for every single 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2025-01-22 04:49:43

据我所知,您希望将列表中的每个输入的“键/值”对存储在KV或映射数组中,

Compose为您提供了函数mutableStateListOf 允许您在组合范围内与可观察的 MutableList 进行交互。

Compose 还为您提供了 mutableStateMapOf 但就您而言,我建议您不要使用它,因为您无法轻松地将键/值映射到文本字段中的 index 请参阅代码注释

这是我的建议:

val data = remember { mutableStateListOf<Pair<String, String>>() }

LazyColumn(Modifier.fillMaxWidth()) {
    itemsIndexed(myListOfTextFields) { index, item ->
        val datum = data[index] // can't do this with mutableStateMapOf ??

        Row(Modifier.fillMaxWidth()) {
            TextField(
                value = datum.first,
                onValueChange = { data[index] = data[index].copy(first = it) },
                singleLine = true
            )
            TextField(
                value = datum.second,
                onValueChange = { data[index] = data[index].copy(second = it) },
                singleLine = true
            )
        }
    }
}

ListPair 组合成 MutableList> 可以让您访问 kotlin 的 lib MutableList>.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 observable MutableList 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:

val data = remember { mutableStateListOf<Pair<String, String>>() }

LazyColumn(Modifier.fillMaxWidth()) {
    itemsIndexed(myListOfTextFields) { index, item ->
        val datum = data[index] // can't do this with mutableStateMapOf ??

        Row(Modifier.fillMaxWidth()) {
            TextField(
                value = datum.first,
                onValueChange = { data[index] = data[index].copy(first = it) },
                singleLine = true
            )
            TextField(
                value = datum.second,
                onValueChange = { data[index] = data[index].copy(second = it) },
                singleLine = true
            )
        }
    }
}

Combining List and Pair into MutableList<Pair<K, V>> gives you access to kotlin's lib MutableList<Pair<K, V>>.toMap() turning Iterable into Map<K, V>

The returned map preserves the entry iteration order of the original collection. If any of two pairs would have the same key the last one gets added to the map.

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文