首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >@IgnoredOnParcel注释不适用于Kotlin中的@Parcelize

@IgnoredOnParcel注释不适用于Kotlin中的@Parcelize
EN

Stack Overflow用户
提问于 2022-11-02 05:43:34
回答 1查看 32关注 0票数 0

在使用Kotlin中的@Parcelize注释时,我试图忽略一个字段,所以我使用@IgnoredOnParcel,

请指导我如何解决这个错误。

代码语言:javascript
运行
复制
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

现在我有了重新创建数据类的计划,是否有更好的解决方案可以忽略字段

代码语言:javascript
运行
复制
@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 注释不适用于我

代码语言:javascript
运行
复制
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

}
EN

回答 1

Stack Overflow用户

发布于 2022-11-02 06:09:02

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

回复

错误:实体和POJO必须有一个可用的公共构造函数。您可以拥有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。公共最终类项目实现android.os.Parcelable {..

空间,如果使用@Ignore,不知道pagedLists,因此必须有一种方法来构造一篇文章,而不提供pagedLists值(因为它没有值或pagedLists字段)。

你可以有这样的东西:-

代码语言:javascript
运行
复制
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字段)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74284808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档