android @IgnoredOnParcel注记不适用于Kotlin中的@Parcelize

gmol1639  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(358)

在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
..........................要忽略房间,则需要使用房间的**@忽略**注解不适用于我

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

}
zd287kbt

zd287kbt1#

若要忽略Room,则需要使用Room的**@Ignore**注解。它会将字段忽略为列,因此不需要知道如何保存字段。

错误:实体和POJO必须有一个可用的公共构造函数。您可以有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。public final class Article实现android.os。
如果使用@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())
  • 这假定您可以构造不带参数的Source(?的值或默认值)

另一种方法是根据Room的columns创建两个类,另一个扩展/嵌入第一个(没有pagedLists字段)类。

相关问题