Orderbychild反向

时间:2017-01-05 16:01:29

标签: android firebase firebase-realtime-database firebaseui

我想通过String Votes在FirebaseRecyclerView中订购我的帖子。它包含一个数字作为字符串。如果我写orderbychild("Votes"),它首先显示最小的数字。

我怎么能转过来?干杯!

4 个答案:

答案 0 :(得分:1)

你不一定转过身来。

假设您的数据如下所示:

{ 
    "polls":  {
        "id1": { "name": "lorem", "votes": 4 }
        "id2": { "name": "ipsum", "votes": 3 }
        "id3": { "name": "dolor", "votes": 5 }
        "id4": { "name": "sit", "votes": 2 }
        "id5": { "name": "amot", "votes": 6 }
    }
}

例如,如果您需要前四名,您可以这样做:

.orderByChild("Votes").limitToFirst(4)

会回来:

{
    "id4": { "name": "sit", "votes": 2 }            
    "id2": { "name": "ipsum", "votes": 3 }
    "id1": { "name": "lorem", "votes": 4 }          
    "id3": { "name": "dolor", "votes": 5 }
}

如果你想要另一端,你可以这样做:

.orderByChild("Votes").limitToLast(4)

回报将是:

{
    "id2": { "name": "ipsum", "votes": 3 }
    "id1": { "name": "lorem", "votes": 4 }          
    "id3": { "name": "dolor", "votes": 5 }
    "id5": { "name": "amot", "votes": 6 }
}

如果你需要所有这些,只需在ui中订购。

答案 1 :(得分:0)

我认为更好的方法是使用reverse()在客户端进行排序。

mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

答案 2 :(得分:0)

快速且相当灵活的方法是使用VotesnegativeVotes将数据编入索引,其中negativeVotes=-Votes

然后你会得到你想要的东西:

orderbychild("negativeVotes")

答案 3 :(得分:0)

下面的代码是我解决此问题的方法。它使用3个整数-最大值,该最大值等于数据类型限制范围内想要的最大值。

00unit, 00wolf, 01man, 20595, 2091996, 03dumbdumb

LeaderboardInput类只是传递给数据库的值的获取器和设置器

int round = 5;
int roundKills = 1000;
int points = 500;
round = round-99999;
roundKills = roundKills-999999;
points = points-99999999;
String oundkills = Integer.toString(roundKills);
String oints = Integer.toString(points);
String ound = Integer.toString(round);

LeaderBoardInput leaderBoardInput = new 
LeaderBoardInput(user,"SM",oundkills,oints,ound);
            

提取值时,您将执行以下操作:

reference.child(user+System.currentTimeMillis()).setValue(leaderBoardInput);

例如,如果您有5票,最大票数为9,999,999, 代码看起来像这样

round = round+99999;
roundKills = roundKills+999999;
points = points+99999999;

其中Votes是getter setter类,而scores变量是Votes类实例的列表 这将在db中订购9999994票,但将5打印到列表 多个值的输出将是5 4 3 2 1而不是1 2 3 4 5