Issue
It's first time using Room Database and I wanted to save my data into Nested Class
Plant Class:
@Entity
data class Plant(
@PrimaryKey var id:Long,
@ColumnInfo (name="plant_name") var name: String,
@ColumnInfo(name ="plant_type") var type: Plant_Category,
@ColumnInfo(name= "plant_image") var image: Int,
) {
}
This is the PlantCategory Class :
@Entity
data class Plant_Category(
@PrimaryKey var id: Int =0,
@ColumnInfo(name="plant_type_name") var type:String ="",
@ColumnInfo(name="plant_type_water")var water_time: String ="",
@ColumnInfo(name="plant_type_details")var details: String = "",){
}
I tried using
@TypeConverter or @SerializedName
But it seems those annotations are not available Perhaps I'm missing a library ?
here is my gradle file:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
//Room database
implementation "androidx.room:room-runtime:2.3.0"
implementation "androidx.room:room-ktx:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
//coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2'
//Viewmodel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
//LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
//ui
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
//views
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
//testing
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
I will be grateful for your help!
Solution
If you are planning to Insert it would be easier to do this:
@Entity
data class Plant(
@PrimaryKey var id:Long,
@ColumnInfo (name="plant_name") var name: String,
@ColumnInfo(name ="plant_type") var type: Plant_Category,
@ColumnInfo(name= "plant_image") var image: Int,
@ColumnInfo(name="plant_type_name") var type:String ="",
@ColumnInfo(name="plant_type_water")var water_time: String ="",
@ColumnInfo(name="plant_type_details")var details: String = "",
) {
}
And if you want to have a specific entity for Category where you insert new plant types you can also have this entity.
@Entity
data class Plant_Category(
@PrimaryKey var id: Int =0,
@ColumnInfo(name="plant_type_name") var type:String ="",
@ColumnInfo(name="plant_type_water")var water_time: String ="",
@ColumnInfo(name="plant_type_details")var details: String = "",){
}
It would be easier for to manage :)
Answered By - MayWheather
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.