【问题标题】:how to convert Source type converter to room如何将源类型转换器转换为房间
【发布时间】:2019-05-16 18:55:43
【问题描述】:

我正在开发一个新闻应用程序并已实现,但我无法将模型类转换为模型类下面的类型转换器

 @Entity
@ForeignKey(entity = Source.class,parentColumns = "source", childColumns = "content")
public class Article {
    @PrimaryKey
    @SerializedName("source")
    @Expose
    @ColumnInfo(name ="source")
    @TypeConverters(SourceTypeConverter.class)
    private Source source;

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    @SerializedName("author")
    @Expose
    @ColumnInfo(name = "author")
    private String author;
    @SerializedName("title")
    @Expose
    @ColumnInfo(name = "title")
    private String title;
    @SerializedName("description")
    @Expose
    @ColumnInfo(name = "description")
    private String description;
    @SerializedName("url")
    @Expose
    @ColumnInfo(name = "url")
    private String url;
    @SerializedName("urlToImage")
    @Expose
    @ColumnInfo(name = "urlToImage")
    private String urlToImage;

    @SerializedName("publishedAt")
    @Expose
    @ColumnInfo(name = "publishedAt")
    private String publishedAt;
    @SerializedName("content")
    @Expose
    @ColumnInfo(name = "content")
    private String content;





    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

在我的 SourceTypeConverter 类下面

public class  SourceTypeConverter {

@TypeConverter
   public static Source ConvertSource(Source source){
   return source ==   null ? null : new Source(source);
}




}

在 Source.java 下面 公共类源{

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;

public Source(Source source) {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

在下面我得到 C:\Users\Edgar\Desktop\Sport News\app\src\main\java\edgar\yodgorbek\sportnews\model\Article.java:18: 错误:无法弄清楚如何保存字段到数据库。您可以考虑为其添加类型转换器。 私有来源来源;

below my database class
@Database(entities = {Article.class}, version = 1, exportSchema = false)
public  abstract class SportNewsDatabase extends RoomDatabase {
  public  abstract SportNewsDao sportNewsDao();
}

【问题讨论】:

  • 请发布您的转换器类以及您需要转换哪些字段?
  • @w201 我无法转换这就是为什么我发布了我的代码请帮助我
  • 什么是源类?现在你的类型转换器根本没有意义。它返回不支持数据库的类型。为 Source 类提供代码
  • @w201 我已经添加了我的数据库我也遇到了错误我也有错误请查看我上面更新的帖子

标签: java android type-conversion android-room


【解决方案1】:

根据我们在linkedin的聊天,我可以说你是个很忘恩负义的人。您的房间无法转换模型中的文件类型源的问题。因此,您需要从 Source 类创建转换器以键入支持的 sqlite。

根据您最新发布的代码,我可以说您尝试做错事。您有两个表:Source 和 Article。并且您想在文章中存储指向 Source 的链接。

  1. 删除您的类型转换器
  2. 阅读此http://androidkt.com/database-relationships/
  3. 简而言之,您需要为文章中的来源创建外键。这是一对一的关系。

【讨论】:

  • 我已添加代码,请检查
  • 请检查我的更新代码,我已经按照你告诉我的方式完成了,但我也遇到了错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多