0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidStudio: Room の使い方 (その2)

Last updated at Posted at 2023-11-07

こちらの記事と同じことを行いました。
AndroidでRoomを使ってデータベース

コードは、次のように取得しました。

git clone https://github.com/cozyk100/AndroidStudioProjectsPublic.git

その中の RoomKotlin というプロジェクトを使いました。

Ksp (Kotlin Symbol Processing API) を使うようにしました。

プロジェクトの作成

プロジェクト名: Room02

環境

build.gradle.kts_Project_Room02
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.1.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}
build.gradle.kts_Module_app
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("com.google.devtools.ksp")
}
(省略)
android {
(省略)
    buildFeatures {
        viewBinding = true
    }
}

dependencies {
    val room_version = "2.6.0"

    //Room

    implementation("androidx.room:room-runtime:$room_version")
    ksp("androidx.room:room-compiler:$room_version")
    implementation("androidx.room:room-rxjava2:$room_version")
    implementation("androidx.room:room-rxjava3:$room_version")
    implementation("androidx.room:room-guava:$room_version")
    testImplementation("androidx.room:room-testing:$room_version")
    implementation("androidx.room:room-paging:$room_version")

    // 非同期
    implementation("io.reactivex.rxjava2:rxjava:2.2.21")
    implementation("io.reactivex.rxjava2:rxkotlin:2.4.0")
    implementation ("io.reactivex.rxjava2:rxandroid:2.1.1")

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.10.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

画面

次は、 RoomKotlin のプロジェクトのものをそのままコピーしました。

activity_main.xml
my_test_view.xml
image.png

プログラム

ツリー構造
image.png

RoomKotlin のプロジェクトのものを
com.example.room -> com.example.room02 と書き変えました。

MainActivity.kt
MyAdapter.kt
dao/DeptDao.kt
dao/UserDao.kt
dao/UserViewDao.kt
db/AppDatabase.kt
entity/Dept.kt
entity/User.kt
entity/UserView.kt

実行結果

image.png

App Inspection の使い方

View -> Tool Windows -> App Inspection
image.png

User_view を表示したところ
image.png

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?