Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Lists with RecyclerViews Adding Methods for a RecyclerView Adapter

Now let's work on onCreateViewHolder(). I started it for you...all you need to do is to create a

am stuck

GameAdapter.java
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

    private Game[] mGames;

    public GameAdapter(Game[] games) {
        mGames = games;
    }

    @Override
    public GameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_list_item, parent, false);
    holder.bindGame(mGames[position]);
    }

    @Override
    public void onBindViewHolder(GameViewHolder holder, int position) {
      GameViewHolder = new GameViewHolder(view); 
        return ViewHolder;  // Task 3
    }

    @Override
    public int getItemCount() {
       return mGames.length; 

    }

    /* Add your code here */
    public class GameViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitleLabel;

        public GameViewHolder(View v) {
            super(v);
            mTitleLabel = (TextView)v.findViewById(R.id.titleLabel);
        }

        public void bindGame(Game game) {
            mTitleLabel.setText(game.getTitle());
        }
    }
}

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

Looks like you have the method bodies for onCreateViewHolder and onBindViewHolder partially swapped.

Move this back to onBindViewHolder:

holder.bindGame(mGames[position]);

And this back to onCreateViewHolder:

GameViewHolder holder = new GameViewHolder(view);
return holder;

If anyone is still unclear here it is

oso

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

    private Game[] mGames;

    public GameAdapter(Game[] games) {
        mGames = games;
    }

    @Override
    public GameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_list_item, parent, false);
        // Task 2
      GameViewHolder holder = new GameViewHolder(view);
return holder;

    }

    @Override
    public void onBindViewHolder(GameViewHolder holder, int position) {
          // Task 3
        holder.bindGame(mGames[position]);
    }

    @Override
    public int getItemCount() {
        // Task 1
        return mGames.length; 
    }

    /* Add your code here */
    public class GameViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitleLabel;

        public GameViewHolder(View v) {
            super(v);
            mTitleLabel = (TextView)v.findViewById(R.id.titleLabel);
        }

        public void bindGame(Game game) {
            mTitleLabel.setText(game.getTitle());
        }
    }
}