Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

How to detect null value or no child values present in FirebaseRecyclerAdapter


I have used Firebase RecyclerViewAdapter extensively in my app but since the below code doesn't fire populateViewHolder without any data to load in the first place, I can't show the load complete message for the initial load

RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));

mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, android.R.layout.two_line_list_item, ChatHolder.class, mRef) {
    @Override
    public void populateViewHolder(ChatHolder chatMessageViewHolder, Chat chatMessage, int position) {
        chatMessageViewHolder.setName(chatMessage.getName());
        chatMessageViewHolder.setText(chatMessage.getText());
    }
};
recycler.setAdapter(mAdapter);

Solution

  • What you're describing is covered by this feature: Handle empty state for recycleview. You can handle this by implementing onDataChanged() in your adapter. A snippet from the sample app of FirebaseUI:

    @Override
    protected void onDataChanged() {
        // if there are no chat messages, show a view that invites the user to add a message
        mEmptyListView.setVisibility(
            mRecyclerViewAdapter.getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE
        );
    }