在使用GSON和SQLight Android应用程序时处理模型类的专业方法
问题描述:
在我的应用程序中,我从Web服务获取数据并在回收站视图中显示这些数据。之后,我打算将这些数据添加到本地sqlite数据库,并显示这些数据时,用户打开应用程序没有互联网连接。在使用GSON和SQLight Android应用程序时处理模型类的专业方法
下面是我使用的GSON
public class Repo implements Parcelable {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
@Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
@Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
我可以创建一个几乎相同的模型类来表示的SQLite数据的简单模型类标准杆JSON结果。在这里我正在使用ORMlite。但对于其他ORM,这种情况非常相似。
@DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo {
public static final String TABLE_NAME = "repo";
@DatabaseField(columnName = "repo_id")
private long repoId;
@DatabaseField(columnName = "name")
private String name;
public long getRepoId() {
return repoId;
}
public String getName() {
return name;
}
public void setRepoId(long repoId) {
this.repoId = repoId;
}
public void setName(String name) {
this.name = name;
}
}
但是,当我试图将这些数据保存到SQLite数据库时,我已经在GSON模型类中设置了数据对象。从GSON模型复制对象并将该值设置为SQLite模型是一件多余的事情。所以我想出了下面的解决方案,尝试使用单个模型类来表示两者。
@DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo implements Parcelable {
public static final String TABLE_NAME = "repo";
@DatabaseField(columnName = "repo_id")
@SerializedName("id")
@Expose
private Integer id;
@DatabaseField(columnName = "name")
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
@Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
@Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
我有不同类型的模型类的地方只有String
类型的字段试试这个。由于GSON使用类型如Integer
,Boolean
阻止我使用SQLite的同一模型,因为数据库不会将Integer
识别为类型,所以为了工作,它需要为int
。
那么,处理这个问题的专业方法是什么?除了回到创建两个独立模型类来表示SQLite和GSON的方法之外,我还没有其他选择。
*由于GSON使用原始类型,如整型,布尔*' Integer'不是原始类型,既不是'Boolean' ... define *数据库不能识别Integer * ... SQLite对java类型一无所知 – Selvin
谢谢指出它。编辑。 –