更新请求不适用于会议室数据库

时间:2019-11-17 15:14:50

标签: java android android-fragments android-room android-jetpack

我正在尝试在编辑时更新数据库。我有一个单击侦听器,用于检查表单输入中的数据是否是来自编辑请求的新数据或自变量,并且工作正常。

click事件有效,但在我填充时数据库上的更新不起作用。

注册片段以进行添加和更新

  override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        arguments?.let {

            contact = ContactEntity(
                it.getString( "fName").toString(),
                it.getString( "lName").toString(),
                R.drawable.maleavatar,
                it.getString( "email").toString(),
                it.getString( "phone").toString(),
                it.getString( "address").toString()
            )

            contact?.let { view ->
                editFirstName.setText(view.firstName)
                editLastName.setText(view.lastName)
                view.image
                editEmail.setText(view.email)
                editPhone.setText(view.phone)
                editAddress.setText(view.address)

            }



        }
        ToastHelper(context!!, "onAct: $contact")


        nav_back_reg.setOnClickListener {
            val backIntent = Intent(context, MainActivity::class.java)
            startActivity(backIntent)
        }


        submitBtn.setOnClickListener {
            val firstName = editFirstName.text.toString()
            val lastName = editLastName.text.toString()
            val email = editEmail.text.toString()
            val phone = editPhone.text.toString()
            val address = editAddress.text.toString()

            if(TextUtils.isEmpty(firstName)) {

                editFirstName.error = "First name is required"
                return@setOnClickListener
            }
            if(lastName.isEmpty() ) {
                editLastName.error = "Last name is required"
                return@setOnClickListener
            }
            if(phone.isEmpty()){
                editPhone.error = "Phone number is required"
                return@setOnClickListener

            }


            context?.let {

                val newContact = ContactEntity(
                    firstName,
                    lastName,
                    R.drawable.maleavatar,
                    phone,
                    email,
                    address
                )
                if(contact == null){

                    MyDatabase(it).getContactDao().add(newContact)
                    ToastHelper(it, "contact saved")

                }
                else{
                    newContact.id = contact!!.id
                    MyDatabase(it).getContactDao().updateContact(newContact)
                    ToastHelper(it, "contact updated")

                }
                val intent = Intent(it, MainActivity::class.java)
                startActivity(intent)

            }




        }

    }

DAO类

@Dao
interface ContactDao {
    @Query("SELECT * FROM contactentity ORDER BY id ASC")
    fun getAllContacts():List<ContactEntity>
    @Insert
    fun add(contact: ContactEntity)
    @Update
    fun updateContact(contact: ContactEntity)
    @Delete
    fun delete(contact: ContactEntity)
}

0 个答案:

没有答案