0

I'm trying to implement a recycler view into my app, but I'm getting the above error. It seems like I'm not setting the adapter in the correct place.

To give you an overview, I need to search the database based on the result of a different query, so I created a method populateViews(query) inside the MainActivity.class to push the result of the query in string format out of the onDataChanged()

I checked the log, and it seems the code is going into the populateView method but not able to find the adapter.

The populateView method is like:

protected void populateView(Query query) {

    menuRecyclerView.setHasFixedSize(true);
    menuRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerOptions<Menu> options;
        FirebaseRecyclerAdapter<Menu, FirebaseViewHolder> adapter;

        options = new FirebaseRecyclerOptions.Builder<Menu>().setQuery(query, Menu.class).build();

        adapter = new FirebaseRecyclerAdapter<Menu, FirebaseViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull FirebaseViewHolder holder, int position, @NonNull Menu model) {

                holder.dishName.setText(model.getDishName());
                holder.dishPrice.setText(model.getDishPrice());

                Log.i("name", model.getDishName());
                Log.i("price", model.getDishPrice());

            }

            @NonNull
            @Override
            public FirebaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new FirebaseViewHolder(LayoutInflater.from(MenuListActivity.this).inflate(R.layout.menu_list_layout, parent, false));
            }
    };

    menuRecyclerView.setAdapter(adapter);

}

EDIT: Added layout manager, but still I'm getting the same error.

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

1 Answers1

0

You need to set layoutmanager before attaching the adapter. Below is the example of layoutmanager that helps populating data in vertical manner.

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
menuRecyclerView.setLayoutManager(linearLayoutManager);
Surbhi Aggarwal
  • 777
  • 8
  • 25