Android 领域显然正在创建列表,但随后说它的大小为 0
Android Realm apparently creating list, but then says it's size is 0
我正在开发一个 android 应用程序,它使用 Realm 作为数据库。
该数据库在用户安装时已经有一些数据。问题是这个 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,我保存这条记录的地方,realm操作基本上是这样的:
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 自动递增并且工作正常,所有肌肉对象都插入数据库中,我可以在我的应用程序中看到它们没问题,但是显然已经创建了例程,因为我看到列表大小变了up 和调试器中的 id 递增,但是当我尝试在我的适配器中访问它时 int workoutsNumber = muscle.getRoutines().size();
, workoutsNumber 变为 0.
我不知道这里有什么问题。在调试中一切看起来都很好,除了一件事我不明白。调试器中的第一件事总是 muscle = cannot find local variable 'muscle'
这是一个屏幕截图,您可以看到正在有效地创建对象,并将例程列表添加到肌肉对象,以及您可以看到我上面提到的错误:debug screenshot
如果列表大小应该是 3,为什么我在执行 int workoutsNumber = muscle.getRoutines().size();
时得到 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);
我正在开发一个 android 应用程序,它使用 Realm 作为数据库。
该数据库在用户安装时已经有一些数据。问题是这个 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,我保存这条记录的地方,realm操作基本上是这样的:
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 自动递增并且工作正常,所有肌肉对象都插入数据库中,我可以在我的应用程序中看到它们没问题,但是显然已经创建了例程,因为我看到列表大小变了up 和调试器中的 id 递增,但是当我尝试在我的适配器中访问它时 int workoutsNumber = muscle.getRoutines().size();
, workoutsNumber 变为 0.
我不知道这里有什么问题。在调试中一切看起来都很好,除了一件事我不明白。调试器中的第一件事总是 muscle = cannot find local variable 'muscle'
这是一个屏幕截图,您可以看到正在有效地创建对象,并将例程列表添加到肌肉对象,以及您可以看到我上面提到的错误:debug screenshot
如果列表大小应该是 3,为什么我在执行 int workoutsNumber = muscle.getRoutines().size();
时得到 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);