我正在嘗試將RealmObject添加到另一個RealmObject中的RealmList中。將RealmObject添加到Android中的RealmList中
所以這就是我現在這樣做的方式。
首先我創建並保存一個名爲 「RouteRealm」 RealmObject這樣的:
public void insertNewRoute(int routeId, long routeDate) {
realm.beginTransaction();
RouteRealm routeRealm = realm.createObject(RouteRealm.class);
routeRealm.setId(routeId);
routeRealm.setDate(routeDate);
realm.commitTransaction();
}
類RealmObject看起來是這樣的:
public class RouteRealm extends RealmObject {
@PrimaryKey
private int id;
private long date;
private RealmList<RoutePointRealm> routePoints;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public RealmList<RoutePointRealm> getRoutePoints() {
return routePoints;
}
public void setRoutePoints(RealmList<RoutePointRealm> routePoints) {
this.routePoints = routePoints;
}
}
上述作品。當我嘗試將RoutePointRealm添加到名爲routePoints的列表中時,會發生此問題。這裏是我加入RoutePointRealm對象添加到列表代碼:
public void insertNewRoutePoint(int routeId, String address, float latitude, float longitude, long routePointId, long routePointTime) {
realm.beginTransaction();
RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo("id", routeId).findFirst();
RoutePointRealm routePointRealm = realm.createObject(RoutePointRealm.class);
routePointRealm.setAddress(address);
routePointRealm.setLatitude(latitude);
routePointRealm.setLongitude(longitude);
routePointRealm.setRoutePointID(routePointId);
routePointRealm.setRoutepointTime(routePointTime);
routeRealm.getRoutePoints().add(routePointRealm);
realm.copyToRealmOrUpdate(routeRealm);
realm.commitTransaction();
}
而且RoutePointRealm看起來是這樣的:
public class RoutePointRealm extends RealmObject {
@PrimaryKey
private long routePointID;
private float longitude, latitude;
private long routepointTime;
private String address;
public long getRoutePointID() {
return routePointID;
}
public void setRoutePointID(long routePointID) {
this.routePointID = routePointID;
}
public float getLongitude() {
return longitude;
}
public void setLongitude(float longitude) {
this.longitude = longitude;
}
public float getLatitude() {
return latitude;
}
public void setLatitude(float latitude) {
this.latitude = latitude;
}
public long getRoutepointTime() {
return routepointTime;
}
public void setRoutepointTime(long routepointTime) {
this.routepointTime = routepointTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
出於某種原因,RoutePointRealm被添加到列表中,但其所有字段被設置爲零並且爲空。 我調試了我的應用程序,以確保所有參數都包含正確的值和正確的數據類型等等。所以現在我知道問題與Realm方法有關。
我希望有人能看到最新的錯誤。任何幫助將不勝感激。