0

我使用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的文档,我错过了什么吗?

提前感谢任何人。

4

1 回答 1

0

您必须重写 save() 方法以将您的 AddressModel 保存在 PlaceModel 中,如下所示:

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

它不漂亮,但它是解决方案之一,因为现在,就像在文档中写的那样:“一对一的关系还不起作用”,所以你必须找到一种解决方法

于 2015-12-21T13:30:33.993 回答