Room & SQLite (Kotlin)

Зависимости:

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
dependencies {

implementation 'androidx.room:room-ktx:2.4.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
kapt 'androidx.room:room-compiler:2.4.3'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.asLiveData
import com.text_game.roomtest.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val db = MainDb.getDb(this)
db.getDao().getAllItem().asLiveData().observe(this){ list->
binding.tvList.text = ""
list.forEach {
val text = "Id: ${it.id} Name: ${it.name} Price: ${it.price}\n"
binding.tvList.append(text)
}
}
binding.button2.setOnClickListener {
val item = Item(null,
binding.edName.text.toString(),
binding.edPrice.text.toString()
)
Thread{
db.getDao().insertItem(item)
}.start()
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Hello World!"
app:layout_constraintBottom_toTopOf="@+id/edName"
app:layout_constraintEnd_toEndOf="@+id/edName"
app:layout_constraintStart_toStartOf="@+id/edName"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.089" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/edPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:ems="10"
android:hint="Price"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="@+id/button2"
app:layout_constraintStart_toStartOf="@+id/button2" />

<EditText
android:id="@+id/edName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/edPrice"
app:layout_constraintEnd_toEndOf="@+id/edPrice"
app:layout_constraintStart_toStartOf="@+id/edPrice" />

</androidx.constraintlayout.widget.ConstraintLayout>

Dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface Dao {
@Insert
fun insertItem(item: Item)
@Query("SELECT * FROM items")
fun getAllItem(): Flow<List<Item>>
}

Item

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity (tableName = "items")
data class Item (
@PrimaryKey(autoGenerate = true)
var id: Int? = null,
@ColumnInfo(name = "name")
var name: String,
@ColumnInfo(name = "price")
var price: String,
)

MainDb

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

@Database (entities = [Item::class], version = 1)
abstract class MainDb : RoomDatabase() {
abstract fun getDao(): Dao

companion object{
fun getDb(context: Context): MainDb{
return Room.databaseBuilder(
context.applicationContext,
MainDb::class.java,
"test.db"
).build()
}
}
}

Добавить комментарий

Ваш адрес email не будет опубликован.