1
@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.

Sehal Sein
  • 11
  • 1
  • 1
  • 3
  • you have to make your own adapter that extends RecyclerViewAdapter, you have to make a layout file that defines how each line of your recyclerview is and then you have to pass the data to recyclerview through the adapter. There are a lot of examples like populating recyclerview with data from MYSQL, JSON or what everl API – Kostas Drak Jan 23 '16 at 10:52

3 Answers3

10

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:

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

Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If the dataset is large, this would load it all at once right? Is there some example with lazy loading (combination of limit to first/last depending on the adapter position would be my guess) ? – Aksel Willgert Feb 12 '17 at 10:05
4

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);
ackushiw
  • 1,726
  • 2
  • 14
  • 15
1

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);
}
HKoncept
  • 153
  • 2
  • 8