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
james white
78,399 PointsAndroid Lists and Adapters Lists with RecyclerViews Creating a RecycleView ViewHolder Challenge
Link to Challenge: http://teamtreehouse.com/library/android-lists-and-adapters/lists-with-recyclerviews/creating-a-recyclerview-viewholder
The first two parts of the challenge are not too bad and
you can generally figure out the needed code for the first two parts of the challenge
Challenge Task 1 of 3
The class below is for a RecyclerView adapter. We need to add a view holder! Create a nested class called GameViewHolder. Make it public and have it extend RecyclerView.ViewHolder. Leave the rest blank for now.
Challenge Task 2 of 3
Next, add one member variable to your new view holder: a public TextView named mTitleLabel.
The last part of the challenge is tricky because of the way the question reads:
if you watch the video (many many times):
For the last challenge, though..
Challenge Task 3 of 3
Finally, add a constructor for GameViewHolder that takes a View as its one parameter. Set mTitleLabel using the ID R.id.titleLabel.
...based on a strict reading of the question,
you may be tempted to use code like this:
/* Add your code here */
public class GameViewHolder extends RecyclerView.ViewHolder {
public TextView mTitleLabel;
public GameViewHolder(View) {
mTitleLabel = View.findViewById(R.id.titleLabel);
}
}
Unfortunately this will not pass task 3 of 3.
What is needed?
Here is the forum thread that gave me the clue:
https://teamtreehouse.com/forum/call-getter-method-in-adapter-class
..which has this code:
public ListItemViewHolder (View itemView){
super(itemView);
nameView = (TextView) itemView.findViewById(R.id.name);
adresseView = (TextView) itemView.findViewById(R.id.adresse);
typView = (TextView) itemView.findViewById(R.id.typ);
bewertungView = (TextView) itemView.findViewById(R.id.bewertung);
itemView.setOnClickListener(this);
} // end constructor
@Override
public void onClick (View v) {
Intent intent = new Intent(sContext,ActivityDetail.class);
// HERE I WANT TO COMMIT MY DATA
sContext.startActivity(intent);
}
Notice the 'View itemView' parameter.
This is what is given in the video about time mark 4:07.
Notice the 'View v' parameter.
This is what is needed to pass the challenge.
If the question had read:
Finally, add a constructor for GameViewHolder that takes a 'View v' as its one parameter.
..that would have been a lot more helpful (and less confusing).
Because changing the setting of the paramter also causes this line
mTitleLabel = View.findViewById(R.id.titleLabel);
..to change to this line:
mTitleLabel = v.findViewById(R.id.titleLabel);
Maybe this was blindingly obvious to most people,
to the ones for who it was not
hopefully this forum post clues them in to what the question was really asking for..
(without giving the whole code answer).
3 Answers
Ben Jakuben
Treehouse TeacherThanks for sharing this feedback! That last step is intended to test this little piece of knowledge, but I'll see if I can make it a little more clear without giving away the answer.
michikoote
20,646 PointsLooks like your codes are mixed up with constructor and textView. Add constructor first and then set textView.
Hints
- GameViewHolder extends ... --- GameViewHolder is a super class.
- mTitleLabel is a textView. You need to cast.
I hope I'm not confusing you.
Ja'Nel Johnson
10,314 PointsI can't get the first challenge to pass.
I'm following the video but I'm confused.
Here's what I have for the first challenge:
public class GameViewHolder extends RecyclerView.Adapter<>{ };
Any help would be appreciated.
Ben Jakuben
Treehouse TeacherCan you paste in all your code? The GameViewHolder class needs to be inside the curly braces of the GameAdapter class. It also needs to extend RecyclerView.ViewHolder, not RecyclerView.Adapter.