Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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()


                    }

                })
}

Thanks for help.

What I have tried:

I try so things but still not get the solution.
Posted
Updated 14-Sep-20 21:54pm
v2
Comments
David Crow 14-Sep-20 22:32pm    
Is it just me, or are there references to "room" and "credit" tables? Did you write all of the above code?
WorldofCode 15-Sep-20 5:05am    
yes i did it
David Crow 15-Sep-20 8:00am    
And you've stepped through the code using the debugger?
[no name] 15-Sep-20 10:00am    
You start by "writing" one piece data; THEN you add all the other stuff. The problem is in your "data access layer" which one would guess is "db."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900