-2

I'm trying to load image and name from Firebase database to my app in RecyclerView but when I run it it is not showing the image and name with no error, here is my code.

  //Load menu
    recycler_menu=(RecyclerView)findViewById(R.id.recycler_menu);
    recycler_menu.setHasFixedSize(true);
    layoutManager= new LinearLayoutManager(this);
    recycler_menu.setLayoutManager(layoutManager);
    loadMenu();

}
  private void loadMenu(){
      FirebaseRecyclerOptions<Category> options = new FirebaseRecyclerOptions.Builder<Category>().setQuery(category, Category.class).build();
          FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter= new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options){
              @Override
              public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);

                  MenuViewHolder viewHolder = new MenuViewHolder(view);

                  return viewHolder;
              }
           @Override
           protected void onBindViewHolder(MenuViewHolder holder, int position, Category model) {
               holder.txtMenuName.setText(model.getName());
               Picasso.with(getBaseContext()).load(model.getImage()).into(holder.imageView);
           }
           };
       recycler_menu.setAdapter(adapter);
   }

My code is running fine but image and name is not showing, blank activity is displaying. Please help.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Shivam Maindola
  • 67
  • 2
  • 10
  • 3
    **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo May 09 '18 at 08:28

2 Answers2

1

Just add adapter.startListening() in your OnStart to start the data listener and adapter.stopListening() in your onStop to stop it by this way :

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

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}
Itoun
  • 3,713
  • 5
  • 27
  • 47
  • comment setHasFixedSize() method. Did you implemented all of the following instructions : https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849 like Alex mamo told you ? – Itoun May 09 '18 at 09:22
0

This is one of the main problems you usually face when you are using Firebase as your database. I could not see where you retrieve your data but the problem is, most probably, Firebase event listeners working asynchronous. In another way, your method returns before you retrieve the data. In my project, I solved this issue by retrieving data on onCreateView or onCreate and then use those values inside other methods. If it is not the case I just simply do the work inside the event listener, if I have to (It will make your code look extremely complicated and unreadable). It would be more helpful if you do more research about Firebase and its asynchronous working style.

If you somehow find a better way to solve this, please comment below so I can use it too. I am still seeking a better way.

Nope
  • 111
  • 2
  • 12