Search code examples
javaandroidfirebasefirebase-realtime-databasefirebaseui

firebaserecycleradapter() in firebaserecycleradapter cannot be applied to


The same error keeps coming up. I'm guessing they changed how it works now but since I'm a noob, I really need help fixing it.

    private void loadMenu() {
        adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>
            (Category.class, R.layout.menu_item, MenuViewHolder.class, category) {

            @NonNull
            @Override
            public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return null;
            }

            @Override
            protected void onBindViewHolder(@NonNull MenuViewHolder viewHolder, int position, @NonNull Category model) {
                viewHolder.txtMenuName.setText(model.getNama());
                Picasso.get().load(model.getImage()).into(viewHolder.imageView);
                final Category clickItem = model;
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        //get Category Id and Send to new Activity
                        Intent foodlist = new Intent(Home.this, FoodList.class);
                        foodlist.putExtra("CategoryId",adapter.getRef(position).getKey());
                        startActivity(foodlist);
                    }
                });
            }

"(Category.class, R.layout.menu_item, MenuViewHolder.class, category)" This line is in red and it states that FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to (a bunch of code here).


Solution

  • The FirebaseRecyclerAdapter binds a Query to a RecyclerView. When data is added, removed, or changed these updates are automatically applied to your UI in real time.

    First, configure the adapter by building FirebaseRecyclerOptions:

     FirebaseRecyclerOptions<Category> options =
                    new FirebaseRecyclerOptions.Builder<Category>()
                            .setQuery(category, Category.class)
                            .build();
    

    Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item.

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
        @Override
        public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.menu_item for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.menu_item, parent, false);
    
            return new MenuViewHolder(view);
        }
    
        @Override
        protected void onBindViewHolder(MenuViewHolder holder, int position, Category model) {
            // Bind the Chat object to the ChatHolder
            // ...
        }
    };
    

    You can find more information here:

    https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#firebaseui-for-realtime-database