我根据其他领域对象创建了一个领域对象( VolleyballPlayer )。 女性对象包含所有排球对象,但男性对象包含一些排球对象。
我想创建 VolleyballPlayer 对象,将女性和男性对象分组为性别。但根据RealmResults,我找不到怎么做。
我的领域对象:
public class Volleyball extends RealmObject {
private int round;
// ... Generated getters and setters ...
}
public class Gender extends RealmObject {
private String gender;
// ... Generated getters and setters ...
}
//...All females play volleyball.
public class Female extends RealmObject {
private Volleyball volleyball;
private int clasp;
// ... Generated getters and setters ...
}
public class Male extends RealmObject {
private Volleyball volleyball;
private int belt;
// ... Generated getters and setters ...
}
新对象:
public class VolleyballPlayer extends RealmObject {
private Volleyball volleyball;
private Gender gender;
// ... Generated getters and setters ...
}
我有allFemale和allMale realmResults但我还没有采取allPlayer realmResults。
RealmResults<Female> allFemales = mRealm.where(Female.class).findAll();
RealmResults<Male> allMales = mRealm.where(Male.class).findAll();
所有女学生都打排球,如何从排球的所有球员(女性)中提取男性球员? 我在下面的代码中编写了一个方法:
public void setAllVolleyballPlayers(Realm mRealm) {
Realm mRealm = getRealm();
mRealm.beginTransaction();
addAllMalePlayers(mRealm);
addAllFemalePlayers(mRealm);
mRealm.commitTransaction();
}
private void addAllMalePlayers(Realm mRealm) {
RealmResults<Male> allMalePlayers = new MalePlayersQueries().getAllMales(mRealm);
mRealm.beginTransaction();
if (allMalePlayers != null && allMalePlayers.size() > 0) {
VolleyballPlayer volleyballPlayer = mRealm.createObject(VolleyballPlayer.class);
Gender gender = mRealm.createObject(Gender.class);
gender.setGender(getString(R.string.male));
volleyballPlayer.setGender(gender);
for (Male male : allMales) {
VolleyballPlayer volleyballPlayer =
mRealm.createObject(VolleyballPlayer.class);
volleyballPlayer.setVolleyball(male.getVolleyball());
}
}
}
private void addAllFemalePlayers(Realm mRealm) {
RealmResults<Female> allFemales = new FemaleQueries().getAllPlayers(mRealm);
VolleyballPlayer volleyballPlayer = mRealm.createObject(VolleyballPlayer.class);
Gender gender = mRealm.createObject(Gender.class);
gender.setGender(getString(R.string.female));
volleyballPlayer.setGender(gender);
////// All female students play volleyball and how can I extract male players from all the players(in females) who play this volleyball and get females
}
我想在表格末尾显示:
**Volleyball Players**
**Female**
Emma
Olivia
Sophia
**Male**
John
Jame
答案 0 :(得分:2)
就此而言,我个人利用“使用鉴别器字段的单表继承”的力量。
public class Volleyball extends RealmObject {
private int round;
@LinkingObjects("volleyball")
private final RealmResults<VolleyBallPlayer> playedBy = null;
// ... Generated getters and setters ...
}
public class VolleyBallPlayer extends RealmObject {
private Volleyball volleyball;
@Index
private String gender;
private int femaleClasp; // 0, or with Integer `null` if male
private int maleBelt; // 0, or with Integer `null` if female
// ... Generated getters and setters ...
}
现在你可以做到
RealmResults<VolleyBallPlayer> femalePlayers = realm.where(VolleyBallPlayer.class)
.equalTo("gender", Gender.FEMALE.name()).findAll();
RealmResults<VolleyBallPlayer> malePlayers = realm.where(VolleyBallPlayer.class)
.equalTo("gender", Gender.MALE.name()).findAll();
甚至
RealmResults<Volleyball> maleVolleyballs = realm.where(Volleyball.class)
.equalTo("playedBy.gender", Gender.MALE.name()).findAll();