Skip to content Skip to sidebar Skip to footer

Android Firebase Database Adding Items With Keys To Recycler View

I was trying to add & show messages dynamically in recycler view but I could not figure out how to send proper data type to it's adapter. In 'getNewMessage(dataSnapshot) ' fun

Solution 1:

You don't need to use your toMap anymore, it is making you more confused on what you're doing since You already have a POJO.

Make a List in your Activity.

ArrayList<MessageModel> myListModel = new ArrayList<MessageModel>();

Do this on your firebase Listener. Make a List on your Activity

This is for Child Added.

@OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {

            MessageModel tempMessage = dataSnapshot.getValue(MessageModel.class);

            myListModel.add(tempRace);

        }

Now That you have a List Model. By using the onChildAdeed by Firebase. Use this to your supply your Adapter Model and apply it.

IMPORTANT FOR FIREBASE. In everyquery for the fire base, if you need to do something AFTER the anything else is done ex: "onChildAdded" You would need to add another listener to THE SAME DatabaseReference in your case. I have no idea, you did not include your activity even your DatabaseReference so I would provide one.

DatabaseReference myReference = firebaseDatabase.getReferane("chat");
myReference.addValueEventListener(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {

            MyMessageRecyclerAdapter myMessageAdapter= newMyMessageRecyclerAdapter(myListModel, listener);

//now you have to apply this to the RecyclerView.
   yourWhateverNameofTheRecyclerview.SetAdapter(myMessageAdater);
        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {

        }
    });

Post a Comment for "Android Firebase Database Adding Items With Keys To Recycler View"