Reputation: 11
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.menu_recycler);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayout = new LinearLayoutManager(this);
linearLayout.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayout);
MenuHomeAdapter menuHomeAdapter = new MenuHomeAdapter(this, createList(7));
recyclerView.setAdapter(menuHomeAdapter);
//FireBase
Firebase.setAndroidContext(this);
// Get a reference to our posts
Firebase ref = new Firebase("LINK");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Menu post = postSnapshot.getValue(Menu.class);
Toast.makeText(MenuHome.this, post.getSNo() + "-" + post.getDishName(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}
how to display the data in the recycler view.I tried some methods but it was not proper.the Toast works but all other option are wrong.I just want to display the data from firebase in a recycler view.
Upvotes: 1
Views: 20463
Reputation: 598847
As helldawg commented, you'll need to create your own adapter that:
onDataChange
calls from FirebaseRecyclerView
when the data changesThe minimum I could come up with quickly is:
public static class MenuHomeAdapter extends RecyclerView.Adapter<MenuViewHolder> {
ArrayList<Menu> items = new ArrayList<>();
public MenuHomeAdapter(Firebase ref) {
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
items.clear();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Menu menu = postSnapshot.getValue(Menu.class);
items.add(menu);
}
notifyDataSetChanged();
}
public void onCancelled(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);
return new MenuViewHolder(view);
}
public void onBindViewHolder(MenuViewHolder holder, int position) {
Menu item = items.get(position);
holder.getTitleView().setText(item.getTitle());
}
public int getItemCount() {
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
Upvotes: 10
Reputation: 153
Why not simply using the FirebaseRecyclerAdapter ?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView messages = (RecyclerView) findViewById(R.id.messages);
messages.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
Chat.class,
R.layout.message,
ChatHolder.class,
ref) {
@Override
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
holder.setName(chat.getName());
holder.setMessage(chat.getMessage());
}
};
messages.setAdapter(mAdapter);
}
Upvotes: 1
Reputation: 1736
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
public class YourViewHolder extends RecyclerView.ViewHolder {
public YourItemBinding binding;
public YourViewHolder(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
public void populateViewHolder(YourViewHolder viewHolder, YourFirebaseItem item, int position) {
YourItemBinding binding = viewHolder.getBinding();
binding.setItem(item);
}
};
recycler.setAdapter(mAdapter);
Upvotes: 4