package petbox.ae.Class
import androidx.room.Entity import androidx.room.PrimaryKey
@Entity(tableName = "products")
data class PostModel(
@PrimaryKey val id : Int? = null,
val images: ArrayList<Data>,
val name: String,
val description: String,
val price: Double?=null )
data class Data(
val id: Int,
val date_created: String,
val date_created_gmt: String,
val date_modified: String,
val src: String, )
and the error i am getting is:
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private final java.util.ArrayList<petbox.ae.Class.Data> images = null;
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. retrofit2.Call<java.util.List<petbox.ae.Class.PostModel>> product, @org.jetbrains.annotations.NotNull()
error: Cannot figure out how to read this field from a cursor.
private final java.util.ArrayList<petbox.ae.Class.Data> images = null;
how to make it read it ?
CodePudding user response:
You need type converter for your List of Data objects.
class DataConverter {
@TypeConverter
fun fromListToString(list: List<*>): String {
val type = object: TypeToken<List<*>>() {}.type
return Gson().toJson(list, type)
}
@TypeConverter
fun toData(dataString: String?): List<Data> {
if(dataString == null || dataString.isEmpty()) {
return mutableListOf()
}
val type: Type = object : TypeToken<List<Data>>() {}.type
return Gson().fromJson(dataString, type)
}
}
And annotate your images field like this:
@TypeConverters(DataConverter.class)
val images: ArrayList<Data>
Here is documentation https://developer.android.com/training/data-storage/room/referencing-data