将RealmObject添加到Android中的RealmList

时间:2015-12-14 21:21:22

标签: android realm

我目前正在尝试将RealmObject添加到另一个RealmObject中的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被添加到列表中,但其所有字段都设置为零和null。 我调试了我的应用程序,以确保所有参数都包含正确的值和正确的数据类型等。所以现在我知道问题与Realm方法有关。

我希望有人能看到错误。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

首先,谢谢你的回答!在改变你提出的解决方案后,我仍然无法让它工作。至少我不这么认为。

我认为它不起作用的原因部分是因为我用我的gui显示零值的错误..这使我进入调试应用程序,但显然调试器总是显示零或null Realm对象的值..至少在我的情况下。 最后,我尝试使用Realm中的获取值创建一个Toast消息,并返回它应该的内容。

所以我认为开始时没有任何问题..调试器只是让我这么想。如果我浪费你的时间,我很抱歉,但我仍然认为如果其他人遇到同样的问题我应该发布这个答案。

答案 1 :(得分:0)

所有对象都由Realm管理,因此您不需要realm.copyToRealmOrUpdate(routeRealm);来电。

答案 2 :(得分:0)

问题来自ID,Realm不支持自动增量行为,所以你应该手动完成。 类似的东西:

beginTransaction()
foo.setId(value)
commitTrasaction()

答案 3 :(得分:0)

我的个人推荐:

public void insertNewRoute(final int routeId, final long routeDate) {
    final RouteRealm routeRealm = new RouteRealm();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            routeRealm.setId(routeId);
            routeRealm.setDate(routeDate);
            realm.insertOrUpdate(routeRealm);    
        }
    });
}

public void insertNewRoutePoint(final int routeId, final String address, final float latitude, 
                       final float longitude, final long routePointId, final long routePointTime) {
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo(RouteRealmFields.ID, routeId).findFirst();
            RoutePointRealm routePointRealm = new RoutePointRealm();
            routePointRealm.setAddress(address);
            routePointRealm.setLatitude(latitude);
            routePointRealm.setLongitude(longitude);
            routePointRealm.setRoutePointID(routePointId);
            routePointRealm.setRoutepointTime(routePointTime);
            routePointRealm = realm.copyToRealmOrUpdate(routePointRealm);
            routeRealm.getRoutePoints().add(routePointRealm);
        }
    });
}