简体   繁体   中英

Issue with room database android

I want to insert some value that i got from my transaction into Room database .

I use the Insert method to save data into room database and also @query to get all data from database .

But the problem is that the data not saved and when i switch between my screens there is no sign of data .

Code :

my table

@Entity (tableName = "credit")
data class Credit(

    @PrimaryKey(autoGenerate = true)
    val id : Int? ,
    var credittext: Long

)

my Dao :

        //credit table dao

        @Query("SELECT * FROM credit ")
        fun creditall () : LiveData<Credit>


        @Insert(onConflict = OnConflictStrategy.REPLACE)
        suspend fun insertcredit (model : Credit)


        @Query("DELETE FROM credit")
       suspend fun deletecredit ()


}

my repository :

    // repository for Credit

    fun getallcredit() = db.GetDao().creditall()

    suspend fun deletallcredit() = db.GetDao().deletecredit()

    suspend fun insertcredit(model : Credit) = db.GetDao().insertcredit(model)


}

Viewmodel Room :


    // this is for credit tb


    fun creditall() = repository.getallcredit()


    fun deletecredit() = CoroutineScope(Dispatchers.IO).launch {


        repository.deletallcredit()

    }


    fun insertcredit(model: Credit) = CoroutineScope(Dispatchers.IO).launch {


        repository.insertcredit(model)


    }

My Room database :

package com.example.ahwazfriut.Room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase


@Database(entities = [RoomTables::class , Credit::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()
    }

}

and this is my activity where i insert and get data :


class Payment_Activity : AppCompatActivity() {

    lateinit var viewmodel: ViewModelRoom

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.payment_activity)


 paymentVerification()

}
 private fun paymentVerification() {


        val textmoney: TextView = findViewById(R.id.money)
        val data: Uri? = intent.data

        val getpurchase = ZarinPal.getPurchase(this)


        getpurchase.verificationPayment(data) {

                isPaymentSuccess, refID, paymentRequest ->

            if (isPaymentSuccess) {


                val database = DataBaseRoom(this)
                val repositoryCart = RepositoryCart(database)
                val factoryRoom = FactoryRoom(repositoryCart)

                viewmodel = ViewModelProvider(ViewModelStoreOwner { ViewModelStore() } , factoryRoom).get(ViewModelRoom::class.java)

                viewmodel.insertcredit(Credit(null , paymentRequest.amount))

                viewmodel.creditall().observe(this, Observer {

                    if (it != null) {
                      
                        textmoney.text = it.credittext.toString()


                    }

                })
}

Thank's for help .

the problem was that i declared the getallcredit method in the wrong place . actually it must be on the on create view to store data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM