android - 我找不到解决方案可以请人
问题描述
我正在做一个笔记应用程序,但我的问题在于
var id: Int? = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle? ) {
super.onViewCreated(view, savedInstanceState)
addbt.setOnClickListener {
var dbManger = DbManger(this!!.requireContext())
val title = ettittel.text.toString()
val beschreibung = etbeschreibung.text.toString()
val values = ContentValues()
values.put("Title", title)
values.put("Beschreibung", beschreibung)
if (id == 0) {
val id = dbManger.insertNotiz(values)
if (id > 0) {
Toast.makeText(this.context, "Text wurde hinzugefügt", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this.context, "Fehlgeschlagen", Toast.LENGTH_LONG).show()
}
} else {
val selectionArgs = arrayOf(id.toString())
val id = dbManger.update(values, "ID=?", selectionArgs)
if (id > 0) {
Toast.makeText(this.context, "Text wurde geändert", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this.context, "änderung Fehlgeschlagen", Toast.LENGTH_LONG).show()
}
}
}
setHasOptionsMenu(true)
val id = arguments?.getInt("ID")
if (id != 0) {
val Title = arguments?.getString("Title")
ettittel.setText(Title)
val beschreibung = arguments?.getString("Beschreibung")
etbeschreibung.setText(beschreibung)
}
}
错误是它永远不会在 else 循环中跳转,所以当我想编辑文本时,它会在我每次尝试几个小时但我无法得到它时添加新文本,我会很高兴得到一个有用的提示,非常感谢提前
解决方案
You have the id
property
var id: Int? = 0
But when you retrieve the id from arguments, you assign it to a local variable
val id = arguments?.getInt("ID")
And your id
property remains unmodified, i.e. equals to 0. To fix this issue you should change the previous line of code to this
id = arguments?.getInt("ID")
推荐阅读
- php - 使用 PHP 的 I 语句和数组
- javascript - React Hooks & UseEffect 不使用 socketIO 数据更新显示
- json - Serilog 到 MongoDB Atlas
- python - 在 DRF 中的帖子上更新点赞
- javascript - 如何将 JSON 配置文件保存到用户的谷歌驱动器并再次检索它?
- android - android库项目中的DI
- django - 来自 Angular8 的发布数据包含空的 QueryDict 但 Django 中 request.body 中的数据
- django - 根据 django ORM 中的喜欢和评论将帖子划分为动态类别
- c++ - 如何使用地图对文本进行编码?
- json - 如何使用 SQL 确定父对象中 JSON 对象的数量