Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Finally, let's tackle onBindViewHolder(). I've added a method in GameViewHolder to bind data to the view. Call it for th

I am really stuck in here, please help! Thanks!

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);
        // Task 2
      GameViewHolder mHolder= new GameViewHolder(view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(GameViewHolder holder, int position) {
        // Task 3
        GameViewHolder(mHolder, position);
    }

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

    /* 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());
        }
    }
}

6 Answers

You just need to do:

holder.bindGame(mGames[position]);

which this calls bindGame on the passed in holder parameter passing into it the Game object in the mGames array at the position given by the position parameter.

You just need to do:

holder.bindGame(mGames[position]);

which this calls bindGame on the passed in holder parameter passing into it the Game object in the mGames array at the position given by the position parameter.

Hello,

For this Task, your onBindViewHolder method is given two parameters, a GameViewHolder called holder, and an integer called position. If you look at the GameViewHolder class, it has a function called bindGame that takes in a game parameter. You want to call this function on the given GameViewHolder and pass it the Game object designated by the position integer that was passed in.

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

What should i change here? Game.bindGame(game, position);

Hello,

In the following code:

    @Override
    public void onBindViewHolder(GameViewHolder holder, int position) {
        // Task 3
        GameViewHolder(mHolder, position);
    }

You need to change GameViewHolder(mHolder, position); You have holder being passed in, and there is the bindGame method for it, which you want to pass into it the element with the index of position of the mGames array.

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

I am trying everything but doesn't work, need more assistance! Thanks!