【问题标题】:How to use orderByChild or orderByValue in a FirebaseRecyclerAdapter如何在 FirebaseRecyclerAdapter 中使用 orderByChild 或 orderByValue
【发布时间】:2016-09-20 03:42:49
【问题描述】:

我正在寻找一种将我的数据分类到 RecyclerView 中的方法。目前我正在为此使用优先级方法,但我知道最好应用orderByChild 或orderByValue 方法来实现此目的。我只是找不到如何正确地做到这一点。

我的(相关)数据结构如下(螺栓格式):

path /events/{event}/GeneralInfo is GeneralInfo{}
path /events/{event}/instances/{instance} is Instance{}

type GeneralInfo {
    owner : KeyString,
    name : ShortNameString | Null, 
    color : Number | Null,
    description : NoteString | Null,
    shared : Boolean | Null,
    startDate : DateString,
    endDate : DateString, 
    instances: Map<KeyString, Boolean>,
    logoAvailable : Boolean | Null
}

type Instance {
    eventKey : KeyString,
    startDateTime : DateString,
    endDateTime : DateString,
    note : String | Null,
}

type KeyString extends String {
    validate() { this.length > 0 && this.length <= 255 }
}

type DateString extends String {
    validate() { this.length == 12 }
}

type ShortNameString extends String {
    validate() { this.length >= 0 && this.length <= 32 }
}

FirebaseRecyclerAdapter 目前初始化如下:

Query q = getGeneralInfo( eventKey ).child( "instances" ).orderByPriority();

mAdapter = new FirebaseRecyclerAdapter<Boolean, InstanceViewHolder>(Boolean.class, R.layout.instance_card, InstanceViewHolder.class, q) {
    @Override
    protected void populateViewHolder(final InstanceViewHolder viewHolder, Boolean model, final int position) {
       getInstanceByKey(mEventKey, refKey).addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
             Instance i = dataSnapshot.getValue(Instance.class);
             ....

请注意,我使用索引键字段(带有布尔值的字段)来检索实际数据。索引字段当前已排序(使用优先级)。

我想直接对实例进行排序(以使事情变得不那么复杂并且能够删除优先级的使用)。

附言尽管看起来数据没有被规范化,但情况确实如此:/events/{event} 路径从未用于检索数据。这样做只是为了保持一些概览。

【问题讨论】:

  • 你目前对每个实例的优先级有什么价值?
  • 优先级设置为:Double.parseDouble( i.getStartDateTime() );
  • DateString 类型的格式如下:201609191200(9 月 19 日,12:00)。

标签: android firebase firebase-realtime-database firebaseui


【解决方案1】:

如果您已经有一个具有您想要订购的价值的孩子,则您不需要优先级。从 cmets 看来,您的优先级与开始日期的值相同。在这种情况下,您也可以按该属性订购:

Query q = getGeneralInfo( eventKey ).child( "instances" ).orderByChild("startDate");

您需要为实例添加索引:

"instances": {
  ".indexOn": "startDate"
}

更新

显然您正在加载 /events/{event}/GeneralInfo/instances,您已将其定义为 instances: Map&lt;KeyString, Boolean&gt;。

由于很明显您要存储一个值,我将其更改为 Map&lt;KeyString, Long&gt;,然后将开始日期的时间戳作为值存储在索引中。

然后您可以使用orderByValue() 以正确的顺序加载/过滤它们。

【讨论】:

  • 嗨弗兰克,感谢您的反应。这适用于我尝试通过查询直接检索对象列表的情况。但在这种情况下,我使用的是索引图。查询getGeneralInfo( eventKey ).child( "instances" ) 返回path /events/{event}/GeneralInfo/instances 类型的路径Map&lt;KeyString, Boolean&gt;。没有子“startDateTime”,并且排序似乎不起作用(在使用具有完整路径的查询时它确实起作用:/events/{event}/instances/{instance})
  • 在我的数据结构中,我使用了许多索引映射。例如,在只有部分数据与其他用户共享的情况下。坚持这个数据结构的一个例子:假设另一个用户有一个索引映射,其中只有路径path /events/{event}/GeneralInfo/instances 中的实例子集。您是否看到在这种情况下应用 orderByChild() 的任何解决方案,也许我需要使用不同的数据结构,或者我必须在这些情况下使用优先级?
  • 啊...在这种情况下,我可以使用 JSON 样本。我添加了一个更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 2021-09-17
相关资源
最近更新 更多