Android 科特林:刚从roomdatabase中得到第一个参数

Android 科特林:刚从roomdatabase中得到第一个参数,android,kotlin,insert,fragment,android-room,Android,Kotlin,Insert,Fragment,Android Room,我的代码有问题,无法解决。 让我们简单介绍一下我将要做的事情:我在ChooseFragment中有一个地址,并且有一个编辑按钮用于编辑地址。好的,到目前为止还不错。编辑点击是将新地址传递到数据库室,地址文本将被更改。但这只是第一次发生。地址未更改的秒结束时间。我知道insert方法可以工作并将新数据发送到数据库室,但当我想通过查询获取数据时,请选择*FROM。。。只显示第一个参数,不替换为新值。我的代码怎么了 这是我的桌子: @Entity (tableName = "addresst

我的代码有问题,无法解决。 让我们简单介绍一下我将要做的事情:我在ChooseFragment中有一个地址,并且有一个编辑按钮用于编辑地址。好的,到目前为止还不错。编辑点击是将新地址传递到数据库室,地址文本将被更改。但这只是第一次发生。地址未更改的秒结束时间。我知道insert方法可以工作并将新数据发送到数据库室,但当我想通过查询获取数据时,请选择*FROM。。。只显示第一个参数,不替换为新值。我的代码怎么了

这是我的桌子:

@Entity (tableName = "addresstable")
data class AddressTb(

    @PrimaryKey(autoGenerate = true)```
    val id : Int? ,
    var address: String)```
这是我的数据库:

@Database(entities = [RoomTables::class , AddressTb::class], version = 1, exportSchema = false)


abstract class DataBaseRoom : RoomDatabase() {

    abstract fun GetDao(): DaoCart


    companion object {
        @Volatile
        private var instance: DataBaseRoom? = null

        private val lock = Any()

        operator fun invoke(context: Context) = instance
            ?: synchronized(lock) {
                instance
                    ?: makeDatabase(
                        context
                    ).also {
                        instance = it
                    }
            }

        private fun makeDatabase(context: Context) = Room.databaseBuilder(
            context.applicationContext,
            DataBaseRoom::class.java,
            "name"
        ).build()
    }

}```
this is my Dao : 
```//address table dao

        @Query("SELECT * FROM addresstable")
        fun getalladress () : LiveData<AddressTb>


        @Insert(onConflict = OnConflictStrategy.REPLACE)
        suspend fun insertaddress (model : AddressTb)


        @Query("DELETE FROM addresstable")
       suspend fun deleteaddress ()

class ManuallyAddressFragment : Fragment() {

    lateinit var viewmodel: ViewModelRoom

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val layoutview = inflater.inflate(R.layout.manually_address_fragment, container, false)

        val database = DataBaseRoom(layoutview.context)
        val repos = RepositoryCart(database)
        val factory = FactoryRoom(repos)

        viewmodel = ViewModelProvider(ViewModelStoreOwner { viewModelStore },
            factory).get(ViewModelRoom::class.java)


        val btncancle: Button = layoutview.btn_cancle
        val btnsubmit: Button = layoutview.btn_submit_address


        btnsubmit.setOnClickListener {

            val edittext = layoutview.edit_address_manually

            if (edittext.text.toString().isEmpty()) {

                Toast.makeText(context, R.string.submit_btn, Toast.LENGTH_SHORT).show()
            } else {


               val  insert = (AddressTb( null , edittext.text.toString()))
                viewmodel.insertaddress(insert)
                childFragmentManager.beginTransaction()
                    .setCustomAnimations(
                        R.anim.anim_fragment_manually,
                        R.anim.anim_fragment_chooseaddress
                    )
                    .replace(R.id.manually_container, ChosseAddress())
                    .commit()
                Toast.makeText(context, "آدرس شما با موفقیت ثبت شد", Toast.LENGTH_SHORT).show()


            }


        }



        btncancle.setOnClickListener {

            childFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.anim_fragment_manually,
                    R.anim.anim_fragment_chooseaddress
                )
                .replace(R.id.manually_container, ChosseAddress())
                .commit()

        }



        return layoutview


    }
}```

这是我的Viewmodel:



    fun getaddress() = repository.getalladdress()


    fun deleteaddres() = CoroutineScope(Dispatchers.Default).launch {


        repository.deleteaddress()

    }


    fun insertaddress(model : AddressTb) = CoroutineScope(Dispatchers.Default).launch {


        repository.insertaddress(model)

这是我获取新插入的片段:

class ChosseAddress : Fragment() {


    lateinit var viewModelRoom: ViewModelRoom

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val vl = inflater.inflate(R.layout.choose_address_layout, container, false)


        val database = DataBaseRoom(requireContext())
        val repository = RepositoryCart(database)
        val factoryRoom = FactoryRoom(repository)


        viewModelRoom =
            ViewModelProvider(ViewModelStoreOwner { ViewModelStore() }, factoryRoom).get(
                ViewModelRoom::class.java
            )
        viewModelRoom.getaddress().observe(viewLifecycleOwner, Observer {


            try {

                vl.txt_address.text = it.address


            } catch (ex: Exception) {
                null
            }

        })



        val animsec: Animation =
            AnimationUtils.loadAnimation(vl.context, R.anim.anim_for_btn_zoom_out)

        vl.button_back_choose_address.setOnClickListener {

            it.startAnimation(animsec)


            childFragmentManager.beginTransaction()
                .replace(R.id.choose_address_container, HamburgerFragment())
                .commit()

        }

        vl.edit_address.setOnClickListener {


            val mycustomview =
                LayoutInflater.from(context).inflate(R.layout.alertfialog_costume, null)
            val dialogtext = LayoutInflater.from(context).inflate(R.layout.edit_alert_txt, null)

            val mBuilder = AlertDialog.Builder(context)
                .setView(mycustomview)
                .setCustomTitle(dialogtext)
            val show = mBuilder.show()

            mycustomview.edit_manually.setOnClickListener {

                show.dismiss()
                childFragmentManager.beginTransaction()
                    .replace(R.id.choose_address_container, ManuallyAddressFragment())
                    .commit()


            }


        }



        return vl


    }


}```
这就是我向数据库插入数据的地方:

@Database(entities = [RoomTables::class , AddressTb::class], version = 1, exportSchema = false)


abstract class DataBaseRoom : RoomDatabase() {

    abstract fun GetDao(): DaoCart


    companion object {
        @Volatile
        private var instance: DataBaseRoom? = null

        private val lock = Any()

        operator fun invoke(context: Context) = instance
            ?: synchronized(lock) {
                instance
                    ?: makeDatabase(
                        context
                    ).also {
                        instance = it
                    }
            }

        private fun makeDatabase(context: Context) = Room.databaseBuilder(
            context.applicationContext,
            DataBaseRoom::class.java,
            "name"
        ).build()
    }

}```
this is my Dao : 
```//address table dao

        @Query("SELECT * FROM addresstable")
        fun getalladress () : LiveData<AddressTb>


        @Insert(onConflict = OnConflictStrategy.REPLACE)
        suspend fun insertaddress (model : AddressTb)


        @Query("DELETE FROM addresstable")
       suspend fun deleteaddress ()

class ManuallyAddressFragment : Fragment() {

    lateinit var viewmodel: ViewModelRoom

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val layoutview = inflater.inflate(R.layout.manually_address_fragment, container, false)

        val database = DataBaseRoom(layoutview.context)
        val repos = RepositoryCart(database)
        val factory = FactoryRoom(repos)

        viewmodel = ViewModelProvider(ViewModelStoreOwner { viewModelStore },
            factory).get(ViewModelRoom::class.java)


        val btncancle: Button = layoutview.btn_cancle
        val btnsubmit: Button = layoutview.btn_submit_address


        btnsubmit.setOnClickListener {

            val edittext = layoutview.edit_address_manually

            if (edittext.text.toString().isEmpty()) {

                Toast.makeText(context, R.string.submit_btn, Toast.LENGTH_SHORT).show()
            } else {


               val  insert = (AddressTb( null , edittext.text.toString()))
                viewmodel.insertaddress(insert)
                childFragmentManager.beginTransaction()
                    .setCustomAnimations(
                        R.anim.anim_fragment_manually,
                        R.anim.anim_fragment_chooseaddress
                    )
                    .replace(R.id.manually_container, ChosseAddress())
                    .commit()
                Toast.makeText(context, "آدرس شما با موفقیت ثبت شد", Toast.LENGTH_SHORT).show()


            }


        }



        btncancle.setOnClickListener {

            childFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.anim_fragment_manually,
                    R.anim.anim_fragment_chooseaddress
                )
                .replace(R.id.manually_container, ChosseAddress())
                .commit()

        }



        return layoutview


    }
}```


我尝试过使用update metohd,但数据库只是返回第一个参数,我想要新的插入值…

对于初学者,如果您获得所有地址,它应该如下所示:

    @Query("SELECT * FROM addresstable")
    fun getalladress () : LiveData<List<AddressTb>>
@Query("SELECT * FROM addresstable WHERE id= :id")
fun findAddressById(id: Long) : AddressTb

要更新该值,请使用相同的id,正如在代码中我看到您尝试插入新对象AddressTbnull、edittext.text.toString。如果要为id传递null值,请从要更新的AddressTb对象传递从getalladdress获得的相同id。

否,我不想要所有地址。我希望新值替换旧值。类似somting的更新。但更新也不起作用,但编写的函数毫无意义,也不起作用。它被称为getAllAddresses,SQLite查询从addresstable中选择所有地址。这意味着它将返回AddressTb对象的列表。那么,我应该使用什么代码从数据库表中返回插入的值呢?不是所有我用你的代码编辑的列表,但不知道我应该在这里传递什么!!!viewModelRoom.getaddress???.observeviewLifecycleOwner,观察者{try{vl.txt_address.text=it.toString}捕获示例:异常{null}传入要获取的地址的id。您有一个满是地址的表,id是唯一标识每个地址的长id。如果需要表中的第一个地址,请使用1。