Java类RecipeDetailed
public class RecipeDetailed extends RealmObject {
@PrimaryKey
private int id;
private String servings;
private String title;
private String image;
private String readyInMinutes;
private boolean vegetarian;
private boolean vegan;
private boolean glutenFree;
private boolean dairyFree;
private RealmList<AnalyzedInstructions> analyzedInstructions;
@Ignore
private ArrayList<Ingredient> extendedIngredients;
public RecipeDetailed() {
}
public RecipeDetailed(int id, String servings, String title, String image, String readyInMinutes, boolean vegetarian, boolean vegan, boolean glutenFree, boolean dairyFree, RealmList<AnalyzedInstructions> analyzedInstructions, ArrayList<Ingredient> extendedIngredients) {
this.id = id;
this.servings = servings;
this.title = title;
this.image = image;
this.readyInMinutes = readyInMinutes;
this.vegetarian = vegetarian;
this.vegan = vegan;
this.glutenFree = glutenFree;
this.dairyFree = dairyFree;
this.analyzedInstructions = analyzedInstructions;
this.extendedIngredients = extendedIngredients;
}
public int getId() {
return id;
}
public String getServings() {
return servings;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
public String getReadyInMinutes() {
return readyInMinutes;
}
public RealmList<AnalyzedInstructions> getAnalyzedInstructions() {
return analyzedInstructions;
}
public boolean isVegetarian() {
return vegetarian;
}
public boolean isVegan() {
return vegan;
}
public boolean isGlutenFree() {
return glutenFree;
}
public boolean isDairyFree() {
return dairyFree;
}
public ArrayList<Ingredient> getExtendedIngredients() {
return extendedIngredients;
}}
Java类收藏夹
public class Favourites extends RealmObject {
private RecipeDetailed recipes;
@PrimaryKey
private int id;
public Favourites() {
}
public Favourites(RecipeDetailed recipes, int id) {
this.recipes = recipes;
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public RecipeDetailed getRecipes() {
return recipes;
}
public void setRecipes(RecipeDetailed recipes) {
this.recipes = recipes;
}}
我保存数据的方式是通过这个
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Favourites favourites = realm.createObject(Favourites.class,recipeDetailed.getId());
favourites.setRecipes(recipeDetailed);
}
});
使用
获取数据favourites = realm.where(Favourites.class).findAll();
Log.i(&#34;数据&#34;,element.get(0).getId()+&#34;&#34;。+ element.get(0).getRecipes()的getTitle() );
问题是第一部分返回正确的数据,即getid返回正确的id但getrecipes返回一个null对象。任何想法我哪里出错了?