2015-09-08 33 views
0

我使用sugarOrm並且我想用地址對象保存一個地方,當我保存所有內容都很好時,但是當我嘗試獲取地址時,它始終爲空,其他來自該地點的數據將被保存我可以得到他們,但我不知道爲什麼我不能得到我的地址對象及其信息。一對一失敗sugarOrm

我的代碼:

public class PlaceModel extends SugarRecord<PlaceModel> implements Parcelable { 
    @Ignore 
    @Expose 
    private ArrayList<CategoryModel> categories = new ArrayList<CategoryModel>(); 
    @Ignore 
    @Expose 
    private ArrayList<MediaContentModel> medias = new ArrayList<MediaContentModel>(); 
    @Ignore 
    @Expose 
    private ArrayList<ReviewContentModel> reviews = new  A ArrayList<ReviewContentModel>(); 
    @Expose 
    private String name; 
    @Expose 
    @Ignore 
    private List<Double> location = new ArrayList<Double>(); 
    @Expose 
    private String website; 
    @Expose 
    private AddressModel address; 
    @SerializedName("global_score") 
    @Expose 
    private Integer globalScore; 
    @SerializedName("popularity_score") 
    @Expose 
    private Integer popularityScore; 
    @SerializedName("quality_score") 
    @Expose 
    private Integer qualityScore; 
    @Expose 
    @Ignore 
    private Object prices; 
    @Expose 
    @Ignore 
    private Object hours; 
    @Expose 
    @Ignore 
    private Object phone; 
    @Expose 
    private String createdAt; 
    @Expose 
    private String updatedAt; 
    @SerializedName("idObject") 
    @Expose 
    private String idPlace; 

    private String listCategory; 

    private String listMedias; 

    private String listReviews; 

    private String listLocation; 

    public PlaceModel() { 
    } 
    // Setters, getters and stuff for parcelable 
} 

地址型號:

public class AddressModel extends SugarRecord<AddressModel> implements Parcelable { 

    @SerializedName("street_address") 
    @Expose 
    private String streetAddress; 
    @SerializedName("city") 
    @Expose 
    private String city; 
    @SerializedName("state") 
    @Expose 
    @Ignore 
    private Object state; 
    @SerializedName("postal_code") 
    private String postalCode; 
    @SerializedName("country") 
    @Expose 
    private String country; 

    public AddressModel(){ 
    } 
    // Setters, getters and stuff for parcelable 
} 

當我保存:

public void getPlaceDetails(String id) 
    { 
     RestClient client = RestClient.getInstance(); 
     propertiesReader = new PropertiesReader(mContext); 
     Properties apiProperties = propertiesReader.loadPropertiesFile("api.properties"); 

     client.createRestClient(apiProperties.getProperty("APIUrl"), mContext); 
     ApiManager apiManager = client.getApiManager(); 
     PlaceModel place = apiManager.getPlaceDetails(id); 
     if (place != null) { 
      Gson gson = new GsonBuilder().create(); 
      place.setListCategory(gson.toJsonTree(place.getCategories()).getAsJsonArray().toString()); 
      place.setListMedias(gson.toJsonTree(place.getMedias()).getAsJsonArray().toString()); 
      place.setListReviews(gson.toJsonTree(place.getReviews()).getAsJsonArray().toString()); 
      place.setListLocation(gson.toJsonTree(place.getLocation()).getAsJsonArray().toString()); 
      places.add(place); 
      place.save(); 
     } 
    } 

如果我試圖讓我的地址得到了NullPointerException。 我跟着sugarOrm的文檔,我錯過了什麼嗎?

在此先感謝任何人。

+0

添加您的完整代碼,您用來嘗試獲取地址。這可能是您的檢索問題。 –

回答

0

您必須覆蓋save()方法來保存PlaceModel您AddressModel像這樣的東西:

if (address != null) { 
    AdressModel model = new AdressModel(address); 
    model.save; 
    adress = model; 
} 
super.save(); 

它不漂亮,但它的解決方案之一,因爲現在,像寫在doc :「一對一的關係不起作用」,所以你必須找到解決辦法