Thank you for response)
i already have this code.
“val myList: LiveData<List> = databasedao.getAll()” - this code shows error - type mismatch (required: LiveData<List>. found LiveData<List> ), because getAll() function returned Entity object (as i understand).
So ill show my code more detaly.
What i whabt to do: launch app, if database is empty - i inserted row with default data from entity. Then i have at UI - button, which make +1 to curent count. Also i have at UI TextView, which should display curent count from database.
My entity:
@Entity(tableName="my_table1")
data class MyEnt (
@PrimaryKey(autoGenerate = true)
var countId: Long =0L,
@ColumnInfo(name ="my_count")
var countNum: Int=0
)
My Dao:
@Dao
interface CountDataBaseDao {
@Insert
suspend fun insert(myentity: MyEnt)
@Update
suspend fun update(myentity: MyEnt)
@Query("Select * from my_table1 where countId=:key")
suspend fun get(key: Long): MyEnt?
@Query("Select * from my_table1 ORDER BY countId DESC LIMIT 1")
suspend fun getCount(): MyEnt?
@Query("Select * from my_table1")
fun getAllCount(): LiveData<List<MyEnt>>
}
My Database:
@Database(entities = [MyEnt::class], version = 1, exportSchema = false)
abstract class RowCountDB : RoomDatabase() {
abstract val countDataBaseDao: CountDataBaseDao
companion object {
@Volatile
private var INSTANCE: RowCountDB? = null
fun getInstance(context: Context): RowCountDB {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
RowCountDB::class.java,
"my_count_database"
)
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
}
return instance
}
}
}
}
My ViewModel:
class TitleViewModel( val databasedao: CountDataBaseDao, application: Application): AndroidViewModel(application) {
private var mydbCount = MutableLiveData<MyEnt?>()
private var _count = MutableLiveData<Int>()
val count : LiveData<Int>
get() = _count
init {
_count.value=0
initilizeMydbCount()
}
var counts: LiveData<List<MyEnt>> = databasedao.getAllCount() //I shiwed data at UI "[MyEnt{coiuntId=1,countNum=0}]", but i also want find //a way how to show just "countNum", not all row
//here i checking when my app is start, if i dont have data at database - i inserted default entity data
private fun initilizeMydbCount(){
viewModelScope.launch {
mydbCount.value= databasedao.getCount()
if (mydbCount.value==null){
databasedao.insert(MyEnt())
mydbCount.value= databasedao.getCount()
}
}
}
fun plusOne(){
_count.value = (_count.value)?.plus(1)
//here i trying to update database row, countNum column with _count.value. But its dont work, data at UI isnt changed...
fun addPlusAtDB(){
viewModelScope.launch {
mydbCount.value?.countNum=_count.value!!
val newCount =mydbCount.value?: return@launch
databasedao.update(newCount)
//now i need update my LiveData, which displayed at UI, but its dont work....
counts = databasedao.getAllCount()
}
}
}
}
My xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="xmlttitleViewModel"
type="android.example.com.rowcounter2.TitleViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.example.com.rowcounter2.TitleFragment">
<Button
android:id="@+id/count_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="@{()-> xmlttitleViewModel.plusOne()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/count_display"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<TextView
android:id="@+id/db_count_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{xmlttitleViewModel.counts.toString()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/count_display" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>