我正在开发新闻应用程序并实施,但无法将模型类转换为模型类下面的typeconverter
@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();
}
答案 0 :(得分:0)
根据我们在linkedin上的聊天,我可以说你是一个非常忘恩负义的人。您的房间无法转换模型中存在的文件类型Source的问题。因此,您需要从Source类创建转换器以键入sqlite支持的类型。
根据您最新发布的代码,我可以说您尝试做错什么。您有两个表:“来源”和“文章”。并且您想将到Source的链接存储在Article中。