如何在Kotlin中的可变地图上添加键和值
我面临一个奇怪的错误,我无法弄清楚为什么会发生这种情况,因为我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为当您初始化对话框时,您正在这样做:
您正在设置这些值一次,在对话框的布局被夸大之后。因此,这些值只是这些
deDittext
s的初始内容。即使用户在其中键入某些内容,keyjson
和valuejson
也永远不会更改。您需要在存储这些值之前立即阅读这些值,因此您正在抓取当前数据:
It's because you're doing this when you initialise the dialog:
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
EditText
s. Even if the user types something into them,keyjson
andvaluejson
are never changed.You need to read those values right before you store them, so you're grabbing the current data: