I updated FirebaseUI from version 2.4.0 to 3.1.0 and FirebaseRecyclerAdapter is not working anymore. Since the parameter changed, I changed my code as README said, but it is not showing me anything.
Following is what I had for version 2.4.0.
` messageRecyclerView = (RecyclerView) findViewById(R.id.messageRecyclerView);
linearLayoutManager = new LinearLayoutManager(this);
//linearLayoutManager.setStackFromEnd(true);
messageRecyclerView.setLayoutManager(linearLayoutManager);
messages_database = database.getReference("messages");
mFirebaseAdapter = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(
Message.class,
R.layout.item_message,
MessageViewHolder.class,
messages_database) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, Message message, int position) {
viewHolder.messageTextView.setText(message.getText());
viewHolder.messengerTextView.setText(message.getName());
viewHolder.timeTextView.setText(message.getTime());
if (message.getPhotoUrl() == null) {
IconicsDrawable iconicsDrawable = new IconicsDrawable(MainActivity.this, Ionicons.Icon.ion_ios_contact_outline).sizeDp(36).color(message.getColor());
viewHolder.messengerImageView.setImageDrawable(iconicsDrawable);
} else {
Glide.with(MainActivity.this)
.load(message.getPhotoUrl())
.into(viewHolder.messengerImageView);
}
}
};
`
And I changed the code above to the one on the bottom for version 3.1.0.
` messageRecyclerView = (RecyclerView) findViewById(R.id.messageRecyclerView);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
messageRecyclerView.setLayoutManager(linearLayoutManager);
messages_database = database.getReference("messages");
FirebaseRecyclerOptions<Message> options = new FirebaseRecyclerOptions.Builder<Message>().setQuery(messages_database, Message.class).build();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(options) {
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message, parent, false);
return new MessageViewHolder(view);
}
@Override
protected void onBindViewHolder(MessageViewHolder viewHolder, int position, Message message) {
viewHolder.messageTextView.setText(message.getText());
viewHolder.messengerTextView.setText(message.getName());
viewHolder.timeTextView.setText(message.getTime());
if (message.getPhotoUrl() == null) {
IconicsDrawable iconicsDrawable = new IconicsDrawable(ChatActivity.this, Ionicons.Icon.ion_ios_contact_outline).sizeDp(36).color(message.getColor());
viewHolder.messengerImageView.setImageDrawable(iconicsDrawable);
} else {
Glide.with(ChatActivity.this)
.load(message.getPhotoUrl())
.into(viewHolder.messengerImageView);
}
}
};`
I feel like I am doing something wrong on the onCreateViewHolder method, but I do not know how to fix the problem. Can someone help me?
in onStart: adapter.startListening()
@AungThu772 is correct. You could also use setLifecycleOwner(this) option to handle your lifecycle automatically. I would recommend reading the docs.
Thank you @AungThu772 and @SUPERCILEX!
Take a look of code, as I have optimized code and solved this problem.
Here is my solution:
RecyclerviewAdapter is not working with new update of firebase-ui-database to 16.0.1
@AungThu772 is correct. You could also use
setLifecycleOwner(this)option to handle your lifecycle automatically. I would recommend reading the docs.
That was a quickest and the best solution.
Most helpful comment
in onStart: adapter.startListening()