‫Let's go back to the chat activity dot Java file and initialize some variables for data binding recycler
‫view adapters and a list.
‫Now let's move to the Oncreate method and initialize these variables.
‫I'll start with the data binding.
‫So binding equals to data binding util dot Setcontentview this AR dot layout dot activity chat.
‫The second thing is the view model.
‫So my view model equals to new view model provider.
‫And we learned about the view model providers in the previous videos this dot get.
‫And here I need to pass an instance of the view model class.
‫Then I need to initialize the recycler view and link it with the data binding.
‫So binding dot recycler view.
‫Remember, we need to display this activity when the user clicks on a chat group in the groups activity.
‫So for that, I need to get the intent that we created in the groups activity.
‫If we open the groups activity, the view model, and in the adapter here you can see that we need to
‫set the group name and we set it the group name to the click the chat group dot get group name.
‫And here we get it from the array list.
‫So this is the key and this is the value.
‫So for that I need to receive this key value pairs.
‫It's very simple I'm using intent.
‫So group name equals to get intent method dot get string extra.
‫And what is the name of the key.
‫It's the group name.
‫This is the key.
‫So give me the key I'll give you the value of the clicked.
‫And I need to get this clicked item.
‫This clicked group and pass it to the view model.
‫My view model dot get messages live data.
‫And here I need to pass the group name.
‫And what is the group name?
‫It's here.
‫Pass the group name.
‫I'm getting the messages of the corresponding child node in the Firebase database.
‫Since it's a live data, I need to use the observe method.
‫Observe this.
‫And here I need to pass Android X and the new observer.
‫So I'm listening to the updated chat messages.
‫Any change in the live data.
‫This will result in getting the updated list of messages.
‫And what we need to get from this chat messages is to update the recycler view with the new messages.
‫For that I need to use messages list equals to new array list and messages list dot add all and get
‫the chat messages list.
‫So here I'm getting the updated.
‫This unchanged method has only one parameter, which is the chat messages of type list of chat messages
‫that is updated to the latest updates in the chat messages.
‫So I'm getting the live data updated version of the chat list user adapter or the adapter new chat adapter.
‫And here I'm passing the message list and the context get application context.
‫Don't miss I need to use the recycler view dot set adapter method and I'll pass my adapter.
‫And here my adapter dot notify data set changed when there is a new message added.
‫I need to let the recycler view scroll automatically to this new latest message added.
‫So for that I need to use latest position integer equals to user adapter dot get item count minus one.
‫Here I'm getting the position of the latest item and recycler view dot smooth.
‫Scroll to position method and the latest position.
‫Here I'm scrolling the recycler view forcing Recycler view to scroll to the latest message added.
‫Then I'm going to use Data Binding dot set view model to this view model I'm passing this variable to
‫this data variable in the data binding.