2017-10-16 17 views
-1

내 응용 프로그램에서 웹 서비스에서 데이터를 가져 와서 해당 데이터를 리사이클 러보기에 표시합니다. 그런 다음 로컬 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]; 
     } 
    }; 
} 

사용하여 갈 거예요 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을 나타내는 두 개의 별도 모델 클래스를 만드는 방법으로 돌아가는 것 이외의 다른 옵션은 없습니다.

+0

* GSON는 정수, 부울과 같은 원시 유형 *'사용하기 때문에 Integer'는 원시 타입이 아니고 Boolean 타입이 아닙니다 ... define * 데이터베이스는 Integer를 식별하지 않습니다 * ... SQLite는 java 타입에 대해 아무것도 모릅니다 – Selvin

+0

감사합니다. 편집 됨. –

답변

0

이 행해져 Yout 접근 방식은 절대적으로 정확하지만 난 당신이 사용하여 쉽게 설명 된 작업을 달성 할 수있는 당신이 바퀴

을 개혁 너무 많은 노력을 가하고 생각 Room