Android - Firebase按值排序

时间:2016-09-11 23:40:16

标签: android firebase firebase-realtime-database

我在Firebase数据库中有一个包含用户分数列表的节点,我想从最高到最低排序列表,只显示前10个分数:

我的排行榜节点:

leader_board
 -$week_id
   -$user_id: score

我的java代码如下:

Query leader_board_query = leader_board_reference.child(week_id).orderByValue().limitToLast(10);

由于一些奇怪的原因,它始终从最低到最高排序。我尝试使用limitToFirst(10)并且没有任何更改,请帮助

2 个答案:

答案 0 :(得分:5)

orderByValue()将始终按升序排序。

您可以使用limitToLast()仅获取已排序项目的最后一部分,然后必须以编程方式还原项目。

示例数据集:

1, 3, 8, 9, 0, 5, 4, 7, 6, 2

orderByValue().limitToFirst(3)应该返回0, 1, 2

orderByValue().limitToLast(3)应返回7, 8, 9,然后将数组恢复为9, 8, 7

注意:这都是未经测试的,请报告结果;)

答案 1 :(得分:1)

我设法解决了这个痛苦的问题。我所要做的就是通过在布局管理器中添加以下代码行来改变我的recyclerview布局。

    Const cdoSendUsingMethod           = "http://schemas.microsoft.com/cdo/configuration/sendusing"
    Const cdoSendUsingPort             = 2
    Const cdoSMTPServer                = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
    Const cdoSMTPServerPort            = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
    Const cdoSMTPConnectionTimeout     = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
    Const cdoSMTPAuthenticate          = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
    Const cdoBasic                     = 1
    Const cdoSendUserName              = "http://schemas.microsoft.com/cdo/configuration/sendusername"
    Const cdoSendPassword              = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
    'Use SSL for the connection (False or True)
    Const cdoSendTLS                   = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"

    ' create CDOSYS objects
    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
    Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")

    'Set our smtp server
    objCDOSYSCon.Fields.Item(cdoSMTPServer) = "smtp.office365.com"
    objCDOSYSCon.Fields.Item(cdoSMTPAuthenticate) = cdoBasic
    objCDOSYSCon.Fields.Item(cdoSendUserName) = "my.email@email.com"
    objCDOSYSCon.Fields.Item(cdoSendPassword) = "password"
    'objCDOSYSCon.Fields.Item(cdoSMTPServerPort) = 587
    objCDOSYSCon.Fields.Item(cdoSendUsingMethod) = cdoSendUsingPort
    objCDOSYSCon.Fields.Item(cdoSendTLS) = True
    objCDOSYSCon.Fields.Item(cdoSMTPConnectionTimeout) = 30

    objCDOSYSCon.Fields.Update

    'Use our new configurations for our mailer
    Set objCDOSYSMail.Configuration = objCDOSYSCon

    strSpecFile = Application("px683_network_downloads_specs") & strSpecFileName

    objCDOSYSMail.From = "to.email@email.com"
    objCDOSYSMail.To = "my.email@email.com"
    objCDOSYSMail.Subject = "A subject"
    objCDOSYSMail.HTMLBody = "Some text for the body"

    'Normal level of importance
    objCDOSYSMail.Send

    set objCDOSYSMail = nothing
    set objCDOSYSCon = nothing

列表反向排序,导致从最高到最低排序。