如何在Kotlin中的可变地图上添加键和值

发布于 2025-02-03 13:58:16 字数 1203 浏览 3 评论 0原文

我面临一个奇怪的错误,我无法弄清楚为什么会发生这种情况,因为我有一个mutableMapof,并且我想在每次添加新项目时都会动态地向其添加键和值。会发生什么,无论它输入多少数据只能存储最后一个项目,如果我硬编码它正常存储的数据,但是在方法的范围内,它不起作用,我不知道为什么?

private fun showDialogBody() {
    val dialog = Dialog(this)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(true)
    dialog.setContentView(R.layout.dialogue_view)
    val bodyKey = dialog.findViewById(R.id.key) as EditText
    val bodyvalue = dialog.findViewById(R.id.value) as EditText
    val addBtn = dialog.findViewById(R.id.dialogDismiss_button) as Button
    val finishBtn = dialog.findViewById(R.id.dialogDismiss_finish_button) as Button

    keyjson = bodyKey.text.toString().trim()
    valuejson = bodyvalue.text.toString().trim()

    jsonMap = mutableMapOf(keyjson to valuejson)

    addBtn.setOnClickListener {

        jsonMap.put(keyjson, valuejson)


        Toast.makeText(this, "${bodyKey.text} Added", Toast.LENGTH_SHORT).show()
        bodyKey.text.clear()
        bodyvalue.text.clear()
    }
    finishBtn.setOnClickListener {
        dialog.dismiss()
        Toast.makeText(this, " number of key value ${jsonMap.size}", Toast.LENGTH_SHORT).show()

    }
    dialog.show()

}

I am facing a strange error I can't figure out why exactly this happens, as I have a mutableMapOf and I want to keep adding keys and values to it dynamically every time they add new items. what happens is that no matter how much data its entered it only stores the last items, If I hard coded the data it stores it normally, but within the scope of the method it doesn't work and I cant figure out why?

private fun showDialogBody() {
    val dialog = Dialog(this)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(true)
    dialog.setContentView(R.layout.dialogue_view)
    val bodyKey = dialog.findViewById(R.id.key) as EditText
    val bodyvalue = dialog.findViewById(R.id.value) as EditText
    val addBtn = dialog.findViewById(R.id.dialogDismiss_button) as Button
    val finishBtn = dialog.findViewById(R.id.dialogDismiss_finish_button) as Button

    keyjson = bodyKey.text.toString().trim()
    valuejson = bodyvalue.text.toString().trim()

    jsonMap = mutableMapOf(keyjson to valuejson)

    addBtn.setOnClickListener {

        jsonMap.put(keyjson, valuejson)


        Toast.makeText(this, "${bodyKey.text} Added", Toast.LENGTH_SHORT).show()
        bodyKey.text.clear()
        bodyvalue.text.clear()
    }
    finishBtn.setOnClickListener {
        dialog.dismiss()
        Toast.makeText(this, " number of key value ${jsonMap.size}", Toast.LENGTH_SHORT).show()

    }
    dialog.show()

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

东北女汉子 2025-02-10 13:58:18

这是因为当您初始化对话框时,您正在这样做:

keyjson = bodyKey.text.toString().trim()
valuejson = bodyvalue.text.toString().trim()

您正在设置这些值一次,在对话框的布局被夸大之后。因此,这些值只是这些deDittext s的初始内容。即使用户在其中键入某些内容,keyjsonvaluejson也永远不会更改。

您需要在存储这些值之前立即阅读这些值,因此您正在抓取当前数据:

addBtn.setOnClickListener {
    val keyjson = bodyKey.text.toString().trim()
    val valuejson = bodyvalue.text.toString().trim()
    jsonMap.put(keyjson, valuejson)

    Toast.makeText(this, "${bodyKey.text} Added", Toast.LENGTH_SHORT).show()
    bodyKey.text.clear()
    bodyvalue.text.clear()
}

It's because you're doing this when you initialise the dialog:

keyjson = bodyKey.text.toString().trim()
valuejson = bodyvalue.text.toString().trim()

You're setting those values once, right after the dialog's layout has been inflated. So those values are just the initial contents of those EditTexts. Even if the user types something into them, keyjson and valuejson are never changed.

You need to read those values right before you store them, so you're grabbing the current data:

addBtn.setOnClickListener {
    val keyjson = bodyKey.text.toString().trim()
    val valuejson = bodyvalue.text.toString().trim()
    jsonMap.put(keyjson, valuejson)

    Toast.makeText(this, "${bodyKey.text} Added", Toast.LENGTH_SHORT).show()
    bodyKey.text.clear()
    bodyvalue.text.clear()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文