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 trialZubeyr Aciksari
21,074 PointsFinally, 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!
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
James Simshaw
28,738 PointsYou 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.
James Simshaw
28,738 PointsYou 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.
James Simshaw
28,738 PointsHello,
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
21,074 PointsWhat should i change here? Game.bindGame(game, position);
James Simshaw
28,738 PointsHello,
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
21,074 PointsI am trying everything but doesn't work, need more assistance! Thanks!