如何从Firebase数据库中以降序获取数据? [英] How to retrieve data from Firebase database in descending order? 查看:198 发布时间:2017/11/10 19:31:43 java android firebase firebase-database 本文介绍了如何从Firebase数据库中以降序获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想知道如果新的firebase数据库已经添加了这个功能,因为我需要它在我的项目中。 结构看起来像这样.. 和ya设置RecyclerView来反转将不会为我工作,因为这会导致它导致它从底部开始。 解决方案 所以,这是我可以想到的最好的解决方案因为@Frank说这可能是重复的,但我最初在他的帮助下解决了它。 因此,我们可以做的是使 timestamp为负,即-20164846476589 然后用一些较高的值999999999999999L (现在我们不会超过offcourse),然后我们得到的将是新时间戳的最低递减值 自动地将在Firebase中降序 字符串NewDescTimeStamp = String .valueOf((9999999999999L +( - 1 * PreviousTimestamp))); mFirebaseRef.child(NewDescTimeStamp).setValue(true); 解决方案 您可以使用以下一种或多种方法检索已知数量的子元素: limitToFirst(),limitToLast(),startAt(),endAt () 也可以使用其中一种排序方法: orderByChild(),orderByValue(),orderByKey() 比将它们存储在一个数据结构(如ArrayList)中,然后使用java.util.Collections.reverse()来反转列表。 使用firebase,您应该执行一些查询逻辑客户端。并非所有在SQL中可用的操作都可以在Firebase上获得。 I was wondering If new firebase database has added this capability because I need it in my project.. The structure looks like this.. and ya setting RecyclerView to reverse wont work for me because that cause it cause it to start from bottom. Solution So, This is the best solution i can possibly think of..For now. As @Frank said this could be the duplicate but I solved it initially with his help. so What we can do is Make the timeStamp negative i.e -20164846476589 and then Subtracting it with Some higher value like 999999999999999L (which for now we are not going to exceed offcourse) and then what we get Would be the lowest descending value for new Timestamp and the data would automagically will be descending in Firebase ie String NewDescTimeStamp= String.valueOf((9999999999999L+(-1 * PreviousTimestamp))); mFirebaseRef.child(NewDescTimeStamp).setValue(true); 解决方案 You can't do that with the existing API. You can use one or more of the following methods to retrieve a known number of children: limitToFirst(), limitToLast(), startAt(), endAt() also you can use one of the ordering methods: orderByChild(), orderByValue() , orderByKey() Than, Store them in a data structure (Such as ArrayList) and then Use java.util.Collections.reverse() to reverse the list. With firebase you should do some of the query logic client side. Not all operations available in SQL are available on Firebase. 这篇关于如何从Firebase数据库中以降序获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 查看全文