FirebaseListAdapter does not push individual elements for chat application - Firebase-Ui 3.1

I am making an Android chat application that uses Google firebase to store messages that users write to each other. To display these messages for users, I read them from the database and organized them into a custom ListView using the ListAdapter. This worked fine until I updated my dependencies, specifically firebase ui, so that:

com.firebaseui:firebase-ui:3.1.0 

Now the code for creating the list adapter does not work:

 adapter = new FirebaseListAdapter<ChatMessage>(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class, R.layout.message, this) { @Override protected void populateView(View v, ChatMessage model, int position) { // Get references to the views of message.xml TextView messageText = (TextView)v.findViewById(R.id.message_text); TextView messageUser = (TextView)v.findViewById(R.id.message_user); TextView messageTime = (TextView)v.findViewById(R.id.message_time); // Set their text messageText.setText(model.getMessageText()); messageUser.setText(model.getMessageUser()); // Format the date before showing it messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime())); } }; 

To fix this problem, I updated the code to meet the new requirements of firebase ui, making the code as follows:

 FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>() .setQuery(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class).setLayout(R.layout.message).build(); adapter = new FirebaseListAdapter<ChatMessage>(options) { @Override protected void populateView(View v, ChatMessage model, int position) { // Get references to the views of message.xml TextView messageText = v.findViewById(R.id.message_text); TextView messageUser = v.findViewById(R.id.message_user); TextView messageTime = v.findViewById(R.id.message_time); // Set their text messageText.setText(model.getMessageText()); messageUser.setText(model.getMessageUser()); // Format the date before showing it messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime())); } }; 

This code now compiles, but the list does not display data. Is there a specific correct way to use the new firebase ui dependency to create a list adapter?

+7
java android firebase firebase-database firebaseui
source share
1 answer

For FirebaseRecyclerAdapter and FirebaseListAdapter to display activity data

You need to use this:

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

Since the FirebaseListAdapter uses a listener to check for changes in the firebase database, to listen to the data you need to add adapter.startListening() inside onStart() to be able to display the data in the list.

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

Check this out for more information: Adapter Life Cycle

Note:

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

+16
source share

All Articles