Nested Array error: Cannot figure out how to save this field into database. You can consider adding a type converter for it

Nested Array error: Cannot figure out how to save this field into database. You can consider adding a type converter for it

这是第一次使用 Room Database,我想将我的数据保存到 Nested Class

工厂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,

    ) {

}

这是植物类别 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 = "",){
}

我试过使用

@TypeConverter  or @SerializedName 

但是好像那些注解不可用 也许我错过了一个图书馆?

这是我的 gradle 文件:

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'

}

我会很感激你的帮助!

如果您打算插入,这样做会更容易:

@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 = "",


    ) {

}

如果您想为插入新植物类型的类别创建一个特定实体,您也可以使用该实体。

@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 = "",){
}

管理起来会更容易:)