0

I have problem with Firebase Recycler Adapter. I am following this tutorial Food Order Tutorial. When I create loadMenu() method and following the steps one by one, Recycler Adapter didn't work exactly same as in video so here's my code.

Can someone help me?

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    FirebaseDatabase database;
    DatabaseReference category;
    TextView txtFullName;
    RecyclerView recycler_menu;
    RecyclerView.LayoutManager layoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Menu");
        setSupportActionBar(toolbar);

        //Init firebase
        database = FirebaseDatabase.getInstance();
        category = database.getReference("Category");

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        //Set Name for User
        View headerView = navigationView.getHeaderView(0);
        txtFullName = (TextView)findViewById(R.id.navHeaderTitle);
        txtFullName.setText(Common.currentUser.getName());


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

        loadMenu();
    }

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

            protected void populateViewHolder(MenuViewHolder viewHolder,Category model, int position){
                viewHolder.txtMenuName.setText(model.getName());
                Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);

                final Category clickItem = model;
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Toast.makeText(Home.this, ""+clickItem.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        recycler_menu.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
  • What doesn't work about the code you shared? – Frank van Puffelen Jun 10 '18 at 17:09
  • new FirebaseRecyclerAdapter(Category.class,R.layout.menu_item,MenuViewHolder.class,category) red line appear under this –  Jun 10 '18 at 17:11
  • it tells me this error: constructor FirebaseRecyclerAdapter in class FirebaseRecyclerAdapter cannot be applied to given types; required: FirebaseRecyclerOptions found: Class,int,Class,DatabaseReference reason: actual and formal argument lists differ in length where T,VH are type-variables: T extends Object declared in class FirebaseRecyclerAdapter VH extends ViewHolder declared in class FirebaseRecyclerAdapter when i run it –  Jun 10 '18 at 17:16
  • edit your question and please Add FirebaseRecyclerAdapter class – SynAck Jun 10 '18 at 17:20
  • Thanks for update. Can you please add the information to the question itself, by clicking the [edit](https://stackoverflow.com/posts/50786108/edit) link right under it? – Frank van Puffelen Jun 10 '18 at 17:21
  • Aside from that: the signature of the constructor has changed. See https://stackoverflow.com/questions/47091837/firebaserecycleradapter-cannot-be-applied-to-firebaserecycleradapter – Frank van Puffelen Jun 10 '18 at 17:22

0 Answers0