仅下载Firebase中数据库参考的一部分?

时间:2018-09-09 16:33:55

标签: java android firebase firebase-realtime-database

我有一个聊天应用程序,作为我的Android应用程序的一部分,该应用程序将所有消息存储在带有Firebase数据库引用的聊天室中,该引用带有该聊天室的ID号。就目前而言,将聊天消息保存在聊天活动中的RecyclerView会在用户每次进入该聊天室时下载该聊天室中的所有消息,从而消耗大量的数据。如果用户滚动到顶部,我应该如何使它只下载X条最新消息并下载X条以上消息?

这是我的适配器:

public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerViewAdapter.ViewHolder> {

private static final String TAG = "ChatRecyclerViewAdapter";
private String mRoomID;
private Context mContext;
private DatabaseReference mDatabaseReference;
private ArrayList<Message> messageList;



private ChildEventListener mListener = new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        // Convert data snapshot from Database into a Message Object

        Message message = dataSnapshot.getValue(Message.class);


        // Add it to an arrayList of Messages
        messageList.add(message);

        // Notice Changes
        notifyItemInserted(messageList.size());

    }

    @Override
    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    }

    @Override
    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
    }
    @Override
    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
};

public ChatRecyclerViewAdapter(Context mContext, ArrayList<String> mMessage, ArrayList<String> mAuthor, String mRoomID, DatabaseReference reference) {

    this.mContext = mContext;
    this.mRoomID = mRoomID;
    messageList = new ArrayList<>();
    mDatabaseReference = reference.child(mRoomID+"_messages");
    mDatabaseReference.addChildEventListener(mListener);

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    //This method loads layout(fields) of ViewHolder

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_msg_row,parent, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    // This method fills fields with data for each list item

    Log.d(TAG,"onBindViewHolder called");

    Message message = messageList.get(position);

    holder.message.setText(message.getMessage());
    holder.author.setText(message.getAuthor()+":");


}

@Override
public int getItemCount() {
    return messageList.size();
}

//Viewholder stores the information about the layout and content of each list item, and serves as a template for each item of a RecyclerView


public class ViewHolder extends RecyclerView.ViewHolder {

    TextView author;
    TextView message;
    RelativeLayout singleMessageContainer;

    public ViewHolder(View itemView) {
        super(itemView);

        author = itemView.findViewById(R.id.chatAuthor);
        message = itemView.findViewById(R.id.chatMessage);
        singleMessageContainer = itemView.findViewById(R.id.singleMessageContainer);

    }
}

void cleanup() {

    mDatabaseReference.removeEventListener(mListener);
}
}

1 个答案:

答案 0 :(得分:2)

如果消息已经按时间顺序存储(例如,如果您使用push()添加消息),则可以按消息的键对消息进行排序,并使用以下命令获取最新消息:

mDatabaseReference = reference.child(mRoomID+"_messages");
Query recentMessages = mDatabaseReference.orderByKey().limitToLast(10);
recentMessages.addChildEventListener(mListener);

如道格所说,请检查Firebase documentation on queries以了解更多选项