在使用Kotlin中的@Parcelize注释时,我试图忽略一个字段,所以我使用@IgnoredOnParcel,
请指导我如何解决这个错误。
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private final com.boltuix.mvvm.model.Source source = null;
^
这里是我的完整源代码: https://github.com/BoltUIX/REST-APIs-with-Retrofit-and-MVVM-architecture。
现在我有了重新创建数据类的计划,是否有更好的解决方案可以忽略字段
@Entity(tableName = "favorite_movie")
@Parcelize
data class Article(
val author: String?,
val content: String?,
val description: String?,
val publishedAt: String?,
val pagedLists: Source,
val title: String?,
val url: String?,
val urlToImage: String?
): Parcelable {
@PrimaryKey(autoGenerate = true)
var id : Int = 0
@IgnoredOnParcel
var pagedList: Source = pagedLists
}
@IgnoredOnParcel注释不适用于Kotlin中的@Parcelize
..若要忽略房间,则需要使用Room的@ ignore 注释不适用于我
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Article implements android.os.Parcelable {..
@Entity(tableName = "favorite_movie")
@Parcelize
data class Article(
val author: String?,
val content: String?,
val description: String?,
val publishedAt: String?,
@Ignore val pagedLists: Source,
val title: String?,
val url: String?,
val urlToImage: String?
): Parcelable {
@PrimaryKey(autoGenerate = true)
var id : Int = 0
}
发布于 2022-11-02 06:09:02
若要忽略Room,则需要使用Room的@Ignore
注释。然后,它将忽略字段作为列,因此不需要知道如何保存字段。
回复
错误:实体和POJO必须有一个可用的公共构造函数。您可以拥有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。公共最终类项目实现android.os.Parcelable {..
空间,如果使用@Ignore
,不知道pagedLists
,因此必须有一种方法来构造一篇文章,而不提供pagedLists
值(因为它没有值或pagedLists
字段)。
你可以有这样的东西:-
constructor(author: String?, content: String?, description: String?, publishedAt: String?, title: String?, url: String?, urlToImage: String?)
: this(author = author, content = content,description =description, publishedAt = publishedAt, title = title, url = url, urlToImage = urlToImage, pagedLists = Source())
?
的或默认值)另一种方法是根据Room的列设置两个类,另一个类扩展/嵌入第一个类(没有pagedLists字段)。
https://stackoverflow.com/questions/74284808
复制相似问题