FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1

Solution 1:

To let the FirebaseRecyclerAdapter and FirebaseListAdapter show the data on the activity

You need to use this:

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}


@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}

Since FirebaseListAdapter uses a listener to check for changes in the firebase database, then to being listening for data you need to add adapter.startListening() inside the onStart() to be able to show the data in the listview.

Then inside onStop() (when activity is stopped), you can use adapter.stopListening() to remove the listener and the data in the adapter.

Check this for more info: Adapter LifeCycle

Note:

If after using the above, you get a nullpointexception or cannot resolve symbol, you have to declare adapter as global variable and please check the below answer: Error in startListening()

Solution 2:

In addition to what's been said in Peter's answer, according to FirebaseRecyclerAdapater latest api documentation, you can create a FirebaseRecyclerAdapter passing in a FirebaseRecyclerOptions instance which is created by a builder. In the builder you specify a lifecycle owner so you don't have to modify either its onStart or its onStop manually:

private fun MainActivity.setUpFirebaseRecyclerAdapter():
    FirebaseRecyclerAdapter<User, ListOnlineViewHolder> {

val options = FirebaseRecyclerOptions.Builder<User>()
        .setQuery(ONLINE_USERS.limitToLast(10), User::class.java)
        .setLifecycleOwner(this)
        .build()

return object : FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(options){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListOnlineViewHolder {
        return ListOnlineViewHolder(
                LayoutInflater.from(parent.context)
                        .inflate(R.layout.user_layout, parent, false))
    }
    override fun onBindViewHolder(holder: ListOnlineViewHolder, position: Int, model: User) {
        holder.bindMessage(model)
    }
}
}

The options builder is made up of a setQuery method which takes in a reference to the db and a model object; the setLifecycleOwner which takes in the activity that will trigger the adapter updates.