Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- Firebase data not showing in ArrayList

I am trying to retrieve data from Firebase database. The top level node is messages and child nodes are author and message. After logging in, the chat messages are pulled from the database. ChatActivity calls the ChatListAdapter with the following code:-

    mAdapter = new ChatListAdapter(this, mDatabaseReference);
    mChatListView.setAdapter(mAdapter);

Here mChatListView is a RecyclerView. The adapter code is as following:-

    public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ViewHolder> {
    private Activity mActivity;
    private DatabaseReference mDatabaseReference;
    private ArrayList<DataSnapshot> mSnapshotList;

    private ChildEventListener mListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mSnapshotList.add(dataSnapshot);
            notifyDataSetChanged();
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView authorName, body;

        public ViewHolder(View view) {
            super(view);
            authorName = (TextView) itemView.findViewById(R.id.author);
            body = (TextView) itemView.findViewById(R.id.message);

        }
    }

    public ChatListAdapter(Activity activity, DatabaseReference ref) {
        this.mActivity = activity;
        this.mDatabaseReference = ref.child("messages");
        mDatabaseReference.addChildEventListener(mListener);
        mSnapshotList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chat_row, parent, false);

        return new ViewHolder(itemView);
    }

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

        InstantMessage message = getItem(position);
        String author = message.getAuthor();
        holder.authorName.setText(author);
        String msg = message.getMessage();
        holder.body.setText(msg);
    }

    public InstantMessage getItem(int i) {
        DataSnapshot snapshot = mSnapshotList.get(i);
        return snapshot.getValue(InstantMessage.class);
    }

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

The ChatActivity is as follows:-

    public class ChatActivity extends AppCompatActivity {

    private RecyclerView mChatListView;    
    private DatabaseReference mDatabaseReference;
    private ChatListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_chat);    

        mDatabaseReference = FirebaseDatabase.getInstance().getReference();           
        mChatListView = (RecyclerView) findViewById(R.id.chat_list_view);        
    }

    @Override
    public void onStart(){
        super.onStart();
        mAdapter = new ChatListAdapter(this, mDatabaseReference);
        mChatListView.setAdapter(mAdapter);
    }

    @Override
    public void onStop() {
        super.onStop();
        mAdapter.cleanUp();
    }
}
like image 339
John Avatar asked Feb 24 '26 23:02

John


1 Answers

There are two ways in which you can solve thos problem. This is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter and this is how you can use an ArrayAdapter.

But note, in order to use those answers you need to add Firebase-UI dependency. See here the latest versions.

like image 71
Alex Mamo Avatar answered Feb 27 '26 13:02

Alex Mamo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!