Android-按时间戳订购Firebase数组 [英] Android - Order Firebase Array by timestamp

查看:64
本文介绍了Android-按时间戳订购Firebase数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android聊天应用程序,在该应用程序中我需要按时间戳排序对话详细信息.我的firebase数据结构在下面提到.

I am developing an android chat application in which I need to order the conversation details by the timestamp. My firebase data structure is mentioned below .

我尝试了以下方法.

方法A

定义数据库参考

Defining DatabaseReference

DatabaseReference conversationReference = mFirebaseDatabaseReference
                 .child(USERS).child(mFirebaseUser.getUid()).child(CONV_DETAILS).orderByChild("last_time_stamp").getRef();

定义FirebaseRecycler适配器

Defining FirebaseRecycler Adapter

 mFirebaseAdapter = new FirebaseRecyclerAdapter<ConversationDetails,
                    ConversationsViewHolder>(
                    ConversationDetails.class,
                    R.layout.conversation_list_item,
                    ConversationsViewHolder.class, conversationReference) {


                @Override
                protected void populateViewHolder(ConversationsViewHolder viewHolder,
                                                  final ConversationDetails conversationDetails, int position) {
}
};

方法B:

在字段名之前加上减号

Prefixing a minus symbol before field name

conversationReference = mFirebaseDatabaseReference
                .child(USERS).child(mFirebaseUser.getUid()).child(CONV_DETAILS).orderByChild("-last_time_stamp").getRef();

这两种情况都不起作用,并且仅按存储在firebase数据库中的数组顺序显示.但是,我需要首先显示具有较高值last_time_stamp的对话详细信息.

Both scenarios do not work and displaying only in the order of array stored in the firebase database.But, I need to display the conversation details with the higher value of last_time_stamp as first.

请任何人帮助我找到解决方案.

Please anyone help me finding the solution.

推荐答案

使用Query查询数据并执行orderByValue().这总是按升序给出数据.因此您可以撤消数据,也可以翻转Recycler视图以显示最新的聊天记录.

Use Query to query the data and perform orderByValue(). This always gives data in ascending order. so you can either reverse the data, or flip the Recycler view to show latest chat.

不要忘记添加限制.使用限制从升序列表中获取最后一个项目.

Do not forget to add limit. Use limit to get that last items from the ascending ordered list.

Query chatQuery = conversationReference.orderByChild("last_time_stamp"). limitToLast(20);

这将以升序时间戳为您提供20个最新的聊天记录.现在翻转此列表并使用它.

This will give you 20 latest chats in ascending order timestamp. Now flip this list and use it.

或者您可以始终在回收者视图中将reverseLayout设置为true. 还要将setStackFromEnd添加到true.

Or you can always set reverseLayout to true in your recycler view. Also add setStackFromEnd to true.

这篇关于Android-按时间戳订购Firebase数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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