How To Retrieve Data To A Recycler View From Firebase
Solution 1:
As helldawg commented, you'll need to create your own adapter that:
- listens to
onDataChange
calls from Firebase - keeps an internal copy of the data from Firebase
- informs the
RecyclerView
when the data changes - creates view holders when those are needed
- binds data from the Firebase data to the view
The minimum I could come up with quickly is:
publicstaticclassMenuHomeAdapterextendsRecyclerView.Adapter<MenuViewHolder> {
ArrayList<Menu> items = new ArrayList<>();
publicMenuHomeAdapter(Firebase ref) {
ref.addValueEventListener(new ValueEventListener() {
publicvoidonDataChange(DataSnapshot snapshot) {
items.clear();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Menu menu = postSnapshot.getValue(Menu.class);
items.add(menu);
}
notifyDataSetChanged();
}
publicvoidonCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
returnnew MenuViewHolder(view);
}
publicvoidonBindViewHolder(MenuViewHolder holder, int position) {
Menu item = items.get(position);
holder.getTitleView().setText(item.getTitle());
}
publicintgetItemCount() {
return items.size();
}
};
To see it in context, look at Activity34962254
(named after the number of your question) on this Github repo.
You'll note that implementing this is fairly non-trivial. And while this implementation works, it is pretty sub-optimal. For example: when a single item is changed/added/deleted, the entire list is rebuilt. That's a recipe for bad performance.
Unless you have specific needs, you're probably better off using the FirebaseRecyclerAdapter
from the FirebaseUI project. To get a full walkthrough of what that offers, I recommend this codelab for building a chat app with FirebaseUI
Solution 2:
There is a pretty good tutorial here
https://github.com/firebase/FirebaseUI-Android#using-firebaseui-to-populate-a-recyclerview
That will give you what you need, and also FirebaseUI gives you a lot to play with.
Now if you want to include databinding as well:
Custom ViewHolder
publicclassYourViewHolderextendsRecyclerView.ViewHolder {
public YourItemBinding binding;
publicYourViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
public YourItemBinding getBinding() {
return binding;
}
}
FirebaseRecyclerAdapter
mAdapter = new FirebaseRecyclerAdapter <YourFirebaseItem, YourViewHolder> (YourFirebaseItem.class, android.R.layout.two_line_list_item, YourViewHolder.class, mRef) {
@Override
publicvoidpopulateViewHolder(YourViewHolder viewHolder, YourFirebaseItem item, int position) {
YourItemBinding binding = viewHolder.getBinding();
binding.setItem(item);
}
};
recycler.setAdapter(mAdapter);
Solution 3:
Why not simply using the FirebaseRecyclerAdapter ?
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerViewmessages= (RecyclerView) findViewById(R.id.messages);
messages.setLayoutManager(newLinearLayoutManager(this));
DatabaseReferenceref= FirebaseDatabase.getInstance().getReference();
mAdapter = newFirebaseRecyclerAdapter<Chat, ChatHolder>(
Chat.class,
R.layout.message,
ChatHolder.class,
ref) {
@OverridepublicvoidpopulateViewHolder(ChatHolder holder, Chat chat, int position) {
holder.setName(chat.getName());
holder.setMessage(chat.getMessage());
}
};
messages.setAdapter(mAdapter);
}
Post a Comment for "How To Retrieve Data To A Recycler View From Firebase"