2015-12-14 24 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); 
     } 
    }); 
} 
相關問題