2017-05-24 80 views
1

我正在开发一个使用Realm作为数据库的Android应用程序。Android Realm显然创建列表,但后来说它的大小是0

当用户安装它时,该数据库中已经有一些数据。问题是这些Realm对象中有一些显然正在创建其他对象的列表,因为我在调试器中看到它,但是当我尝试访问此列表时,如realmObject.getList.size();所得到的输出为0.

更具体地说,我有这样的模式:

public class Muscle extends RealmObject{ 

    @PrimaryKey 
    private int id; 

    private String name; 

    private RealmList<Routine> routines; 

    public Muscle(String name){ 
     this.id = MyApplication.MuscleID.incrementAndGet(); 
     this.name = name; 
     this.routines = new RealmList<Routine>(); 
    } 

    public Muscle(){} 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public RealmList<Routine> getRoutines() { 
     return routines; 
    } 

} 

这一个:

public class Routine extends RealmObject { 

    @PrimaryKey 
    private int id; 

    @Required 
    private String name; 

    private RealmList<Detail> details; 

    public Routine(String name){ 
     this.id = MyApplication.RoutineID.incrementAndGet(); 
     this.name = name; 
     this.details = new RealmList<Detail>(); 
    } 

    public Routine(){} 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public RealmList<Detail> getDetails() { 
     return details; 
    } 

} 

的记录被插入的境界,因为我可以访问,并在我的应用程序显示出来(至少是肌肉记录)我可以在调试器中看到Routine列表也在创建中,但我无法访问每个肌肉套路。

在MyApplication.java,在那里我保存这个记录,境界操作基本上是这样的:

public class MyApplication extends Application { 

    public DateFormat df; 

    public static AtomicInteger MuscleID = new AtomicInteger(); 
    public static AtomicInteger RoutineID = new AtomicInteger(); 
    public static AtomicInteger DetailID = new AtomicInteger(); 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Realm.init(getApplicationContext()); 
     setUpRealmConfig(); 

     Realm realm = Realm.getDefaultInstance(); 
     MuscleID = getIdByTable(realm, Muscle.class); 
     RoutineID = getIdByTable(realm, Routine.class); 
     DetailID = getIdByTable(realm, Detail.class); 

     realm.close(); 
    } 

    private void setUpRealmConfig() { 
     RealmConfiguration config = new RealmConfiguration.Builder().initialData(new Realm.Transaction() { 
      @Override 
      public void execute(Realm realm) { 

      Muscle chest = new Muscle(getString(R.string.chest)); 
      realm.copyToRealmOrUpdate(chest); 

      Muscle legs = new Muscle(getString(R.string.legs)); 
      realm.copyToRealmOrUpdate(legs); 


      Routine cr1 = new Routine(getString(R.string.beginner_chest_1)); 
      realm.copyToRealmOrUpdate(cr1); 

      chest.getRoutines().add(cr1); 

      } 
     }) 
       .deleteRealmIfMigrationNeeded() 
       .build(); 
     Realm.setDefaultConfiguration(config); 
    } 

还有更精彩的,但是这是相关代码。我已经确定每个模型的ID都会自动增加,并且工作正常,所有肌肉对象都被插入到数据库中,并且我可以在我的应用程序中看到它们没有问题,但是这些例程显然是创建的,因为我看着列表大小去了在调试器中使用id增量,但是当我尝试在像int workoutsNumber = muscle.getRoutines().size();这样的适配器中访问它时,锻炼次数变为0.

我不知道问题在这里。除了一个我不明白的地方,在调试中一切看起来都很好。在调试的第一件事始终是muscle = cannot find local variable 'muscle'

这里被有效地创建一个截图,你可以看到的对象,程序列表被添加到肌肉对象,以及你可以看到我上面提到的错误:debug screenshot

那么,为什么我在做int workoutsNumber = muscle.getRoutines().size();时得到0如果列表大小应该是3?

回答

0
 Routine cr1 = new Routine(getString(R.string.beginner_chest_1)); 
     realm.copyToRealmOrUpdate(cr1); 

     chest.getRoutines().add(cr1); 

应该

 Muscle chest = new Muscle(getString(R.string.chest)); 
     Muscle managedChestProxy = realm.copyToRealmOrUpdate(chest); 


     Routine cr1 = new Routine(getString(R.string.beginner_chest_1)); 
     Routine managedRoutineProxy = realm.copyToRealmOrUpdate(cr1); 

     managedChestProxy.getRoutines().add(managedRoutineProxy); 
+0

工作就像一个魅力!非常感谢。 –

相关问题