我正在尝试显示2个领域对象组合的有序/已过滤列表(通过RealmBaseAdapter或RealmRecyclerViewAdapter)。这是我的领域对象类:
第一个对象:
public class TaskModel implements RealmModel {
@Required
@PrimaryKey
private String id;
@Required
private String name;
private Date dueDate;
private String category
private RealmList<SubTaskModel> subTasks;
......getters and setters
}
第二个对象:
public class SubTaskModel implements RealmModel {
@Required
@PrimaryKey
private String id;
@Required
private String name;
private Date dueDate;
private String category
TaskModel task;
......getters and setters
}
我的&#34;任务&#34;和&#34;子任务&#34;对象是一对多关系,这就是我在单独的Object类中建模它们的原因。我想要做的是显示Task和SubTasks的组合列表,并按截止日期对它们进行排序。我还要求按名称排序并按类别和名称过滤(搜索)。即。
返回的有序列表应为:
我倾向于follow this approach,但希望确保这是性能和最佳实践方面的最佳解决方案。从我收集到的,我将不得不创建另一个对象类,让我们说&#34; AllTasks&#34;每次我创建一个Task或SubTasks对象时,我都必须在这里创建一个AllTasks对象链接到它们。此外,每当我更新SubTasks和/或Tasks中的名称,类别和日期字段时,我都必须使用相同的字段更新AllTasks对象。想要确保没有更好的方法来实现我的要求(即每次都要记得更新2个对象,这不是理想的)。
public class AllTasks extends RealmObject {
private static final int ENTRY_TASK = 0;
private static final int ENTRY_SUBTASK = 1;
/** The tag describes what kind of entry it represents */
private int tag;
/* Only one of these can be set, according to what this entry represents. */
@Nullable private TaskModel task;
@Nullable private SubTaskModel subTask;
private Date date;
private String name;
private String category;
public Date getDate() {
return date;
}
/* Can only be accessed from within the 'data' package */
void setDate(Date date) {
this.date = date;
}
......other getter and setters
static AllTasks createAsTask(@NonNull final Realm realm, @NonNull
final TaskModel task) {
if(realm == null) {
throw new IllegalArgumentException("'realm' may not be null");
}
if(mtask == null) {
throw new IllegalArgumentException("'task' may not be
null");
}
AllTasks entry = realm.createObject(AllTasks.class);
entry.tag = ENTRY_TASK;
entry.task = task;
return entry;
}
...repeat above method for SubTasks
}