2015-12-14 60 views
0

我正在尝试将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方法有关。

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

回答

1

首先,感谢您的答案!在改变您提出的解决方案后,我仍然无法实现它。至少我不这么认为。

我认为它不起作用的原因部分是因为我用我的GUI显示零值时犯的一个错误。这让我去调试应用程序,但显然调试器总是显示零或Realm对象的空值。至少在我的情况下。 所以最后,我试着用一个来自Realm的取值的Toast消息,它返回了它应该做的。

所以我不认为有任何问题开始。调试器让我这么想。如果我浪费了你的时间,我很抱歉,但如果其他人遇到同样的问题,我仍然认为我应该将此作为答案。

0

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

0

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

beginTransaction() 
foo.setId(value) 
commitTrasaction() 
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); 
     } 
    }); 
}