java Android领域关系返回null
我有一个名为ProductsDetailInformation
的模型类作为RealmObject,在这个模型中,我有一个字段作为
private RealmList<ProductSlideShowImages> slideShowImages;
我想从ProductSlideShowImages
模型中获取此产品图像,但我的此测试返回[0]
,例如:
ProductsDetailInformation = [
{id: 57f72549-53e1-4177-9e52-80b8421559b7},
{marketId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a},
{productId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a-123456},
{weight: 10},
{cost: 1000},
{color: blue},
{madeIn: china},
{gender:},
{sex:},
{quality: low},
{size: 42},
{sizeModel: xxl},
{count: 10},
{rating: 2},
{description: my description},
{created_at: 2016 - 07 - 17},
{updated_at: 2016 - 07 - 14},
{slideShowImages: RealmList < ProductSlideShowImages > [0]}
]
在这两种情况下marketId
和productId
的数据相同
ProductsDetailInformation
类:
public class ProductsDetailInformation extends RealmObject{
@PrimaryKey
private String id;
private String marketId;
private String productId;
private String weight;
private String cost;
private String color;
private String madeIn;
private String gender;
private String sex;
private String quality;
private String size;
private String sizeModel;
private String count;
private String rating;
private String description;
private String created_at;
private String updated_at;
/**
* RelationShip columns with ProductSlideShowImages table
*/
private RealmList<ProductSlideShowImages> slideShowImages;
/*SETTER AND GETTER*/
}
ProductSlideShowImages
类:
public class ProductSlideShowImages extends RealmObject{
@PrimaryKey
private String id;
private String marketId;
private String productId;
private String imageFileName;
private String created_at;
private String updated_at;
/*SETTER AND GETTER*/
}
通过查询获取结果:
List<ProductsDetailInformation> productsDetails =
realm.where(ProductsDetailInformation.class).equalTo("marketId",marketUUid).findAll();
ProductSlideShowImages
模型和ProductsDetailInformation
不一起填充,它们是在不同的地点和时间填充的
编辑:
下面是我将信息保存到ProductSlideShowImages模型的操作
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ProductSlideShowImages slideShowImages = new ProductSlideShowImages();
try {
//@formatter:off
slideShowImages.setId ( UUID.randomUUID().toString() );
slideShowImages.setMarketId ( jsonObject.getString ( "marketId" ));
slideShowImages.setProductId ( jsonObject.getString ( "productId" ));
slideShowImages.setImageFileName ( jsonObject.getString ( "imageFileName" ));
slideShowImages.setCreated_at ( jsonObject.getString ( "created_at" ));
slideShowImages.setUpdated_at ( jsonObject.getString ( "updated_at" ));
//@formatter:on
realm.copyToRealm(slideShowImages);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
在其他方法中使用下面的事务,我将数据保存到ProductsDetailInformation
模型:
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ProductsDetailInformation products = new ProductsDetailInformation();
try {
//@formatter:off
products.setId ( UUID.randomUUID().toString() );
products.setMarketId ( jsonObject.getString ( "marketId" ));
products.setProductId ( jsonObject.getString ( "productId" ));
products.setWeight ( jsonObject.getString ( "weight" ));
products.setCost ( jsonObject.getString ( "cost" ));
products.setColor ( jsonObject.getString ( "color" ));
products.setMadeIn ( jsonObject.getString ( "madeIn" ));
products.setGender ( jsonObject.getString ( "gender" ));
products.setSex ( jsonObject.getString ( "sex" ));
products.setQuality ( jsonObject.getString ( "quality" ));
products.setSize ( jsonObject.getString ( "size" ));
products.setSizeModel ( jsonObject.getString ( "sizeModel" ));
products.setCount ( jsonObject.getString ( "count" ));
products.setRating ( jsonObject.getString ( "rating" ));
products.setDescription ( jsonObject.getString ( "description" ));
products.setCreated_at ( jsonObject.getString ( "created_at" ));
products.setUpdated_at ( jsonObject.getString ( "updated_at" ));
//@formatter:on
realm.copyToRealm(products);
} catch (JSONException e) {
e.printStackTrace();
Log.e("Error ", " parse json objects");
}
}
});
共 (0) 个答案