
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")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "text_table") | |
data class TextEntity ( | |
@PrimaryKey(autoGenerate = true) | |
@ColumnInfo(name = "id") | |
var id : Int, | |
@ColumnInfo(name = "text") | |
var text : String | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() | |
} |
'Android Jetpack' 카테고리의 다른 글
Room 의 다양한 테크닉들 - 3 (addMigrations) (0) | 2023.02.04 |
---|---|
Room 의 다양한 테크닉들 - 2 (fallbackToDestructiveMigration) (0) | 2023.02.04 |
Room Advanced - 6 (Update / Delete) (0) | 2023.02.02 |
Room Advanced - 5 (Adapter) (0) | 2023.02.02 |
Room Advanced - 4 (Create / Read) (0) | 2023.02.02 |