Android Jetpack

Room 의 다양한 테크닉들 - 1 (Simple Ex)

----___<<<<< 2023. 2. 4. 12:43

 

 

id 'kotlin-kapt'
// ROOM
def roomVersion = "2.5.0"

implementation("androidx.room:room-runtime:$roomVersion")
annotationProcessor("androidx.room:room-compiler:$roomVersion")

// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$roomVersion")

// Coroutine
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

 

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val db = TextDatabase.getDatabase(this)
val inputArea = findViewById<EditText>(R.id.textInputArea)
val insertBtn = findViewById<Button>(R.id.insert)
val getAllBtn = findViewById<Button>(R.id.getData)
val deleteBtn = findViewById<Button>(R.id.delete)
insertBtn.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
db.textDao().insert(TextEntity(0, inputArea.text.toString()))
inputArea.setText("")
}
}
getAllBtn.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
Log.d("MAIN", db.textDao().getAllData().toString())
}
}
deleteBtn.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
db.textDao().deleteAllData()
}
}
}
}
view raw MainActivity.kt hosted with ❤ by GitHub
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/textInputArea"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="text"
android:layout_margin="10dp"
android:background="@android:color/transparent"/>
<Button
android:id="@+id/insert"
android:text="insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
<Button
android:id="@+id/getData"
android:text="getData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
<Button
android:id="@+id/delete"
android:text="delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
@Database(entities = [TextEntity::class], version = 1)
abstract class TextDatabase : RoomDatabase() {
abstract fun textDao() : TextDao
companion object {
@Volatile
private var INSTANCE : TextDatabase? = null
fun getDatabase(
context : Context
) : TextDatabase {
return INSTANCE ?:synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
TextDatabase::class.java,
"text_database"
)
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
instance
}
}
}
}
view raw TextDatabase.kt hosted with ❤ by GitHub
@Entity(tableName = "text_table")
data class TextEntity (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id : Int,
@ColumnInfo(name = "text")
var text : String
)
view raw TextEntity.kt hosted with ❤ by GitHub
@Dao
interface TextDao {
@Query("SELECT * FROM text_table")
fun getAllData() : List<TextEntity>
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(text : TextEntity)
@Query("DELETE FROM text_table")
fun deleteAllData()
}
view raw TextDao.kt hosted with ❤ by GitHub