如何在FirebaseRecyclerAdapter中使用orderByChild或orderByValue [英] How to use orderByChild or orderByValue in a FirebaseRecyclerAdapter

查看:134
本文介绍了如何在FirebaseRecyclerAdapter中使用orderByChild或orderByValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将我的数据分类到RecyclerView中的方法。在这一刻我使用优先方法,但我知道最好应用 orderByChild orderByValue 方法来实现这一点。

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

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

类型GeneralInfo {
所有者:KeyString,
名称:ShortNameString |空,
color:Number |空,
描述:NoteString |空,
shared:Boolean | null,
startDate:DateString,
endDate:DateString,
实例:Map< KeyString,Boolean> ;,
logoAvailable:Boolean | Null


类型实例{
eventKey:KeyString,
startDateTime:DateString,$ b $ endDateTime:DateString,
注意:String |空,
}

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

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

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

FirebaseRecyclerAdapter目前正在初始化如下:

 查询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 $ b $ public void onDataChange(DataSnapshot dataSnapshot){
实例i = dataSnapshot.getValue(Instance.class);
...

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

我想直接对实例进行排序(使事情变得更简单,并能够删除优先级的使用)。
$ b $ p尽管看起来数据没有被标准化,这是情况:/ events / {event}路径永远不会用于检索数据。这只是为了保持一些概述。

解决方案

如果您已经拥有一个值为你想订购。从评论看来,你的优先级是与开始日期相同的价值。在这种情况下,您还可以通过该属性进行排序:

pre $ q $ getGeneralInfo(eventKey).child(instances)。 orderByChild( 的startDate);

您需要为实例添加一个索引:

 instances:{
.indexOn:startDate
}
pre>

更新



显然你正在加载 / events / {event} / GeneralInfo / instances ,您定义为个实例:Map< KeyString,Boolean>



由于很显然你正在寻找一个值,所以我会把它改成 Map< KeyString,Long> 然后将开始日期的时间戳存储为索引中的值。

然后,您可以按照正确的顺序加载/过滤 orderByValue ()


I'm looking for a way to get my data sorted into a RecyclerView. At this moment I'm using the priority method for this, but I know that it is better to apply a orderByChild or orderByValue method to achieve this. I just cannot find how to do this properly.

My (relevant) data structure is the following (bolt format):

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 }
}

The FirebaseRecyclerAdapter is currently initialized as follows:

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);
             ....

Note that I use an index key field (the field with the Boolean) to retrieve the actual data. The index field is currently sorted (using priorities).

I would like to sort directly on the instances (to make things less complicated and be able to remove the use of priorities).

p.s. Although it may seem that the data is not normalized, this is the case: the /events/{event} path is never used to retrieve data. This is only done to keep some overview.

解决方案

You don't need priorities if you already have a child with the value that you want to order on. From the comments it seems that your priority is the same value as the start date. In that case you can also order by that property:

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

You'll need to add an index to the instances:

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

Update

Apparently you're loading /events/{event}/GeneralInfo/instances, which you've defined as instances: Map<KeyString, Boolean>.

Since it's clear that you're looking to store a value, I'd change this to a Map<KeyString, Long> and then store the timestamp of the start date as the value in the index.

Then you can load/filter them in the correct order with orderByValue().

这篇关于如何在FirebaseRecyclerAdapter中使用orderByChild或orderByValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆