首页 > 解决方案 > 我找不到解决方案可以请人

问题描述

我正在做一个笔记应用程序,但我的问题在于

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 循环中跳转,所以当我想编辑文本时,它会在我每次尝试几个小时但我无法得到它时添加新文本,我会很高兴得到一个有用的提示,非常感谢提前

标签: androidandroid-studiokotlin

解决方案


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

推荐阅读