2017-10-16 7 views
-1

私のアプリケーションでは、Webサービスからデータを取得し、それらのデータをリサイクラビューで表示します。その後、これらのデータをローカルのsqliteデータベースに追加して、インターネットに接続していないユーザーがアプリケーションを開いたときにそれらのデータを表示しようとしています。GSONとSQLightのAndroidアプリケーションを使用するときにモデルクラスを処理するプロフェッショナルな方法

は、ここで私はSQLiteのデータを表現するためにほぼ同じモデルクラスを作成することができます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]; 
     } 
    }; 
} 

使用PARS 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データベースはIntegerを型として識別しないので、SQLite用に同じモデルを使用することができなくなるので、作業するためにはintである必要があります。

これを処理するプロフェッショナルな方法は何ですか? SQLiteとGSONを表すために2つの別々のモデルクラスを作成する方法に戻る以外の選択肢はありません。

+0

* GSONは整数、ブールのようなプリミティブ型を* '使用していますので、 Integer'はプリミティブ型ではなく、 'Boolean' ... define *データベースはIntegerを識別しません* ... SQLiteはjava型について何も知らない – Selvin

+0

ありがとうございます。編集されました。 –

答えて

0

Youtのアプローチが絶対に正しいですが、私はあなたが簡単に使用して説明したタスクを達成することができますが、ホイール

を再発明あまりにも多くの努力を入れていると思うRoom

関連する問題