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 trialalainstudio
4,493 PointsAndroid Tapping on a listview item. challenge 2. Call it with the correct game from the list.
Now we can do something with the item that was tapped on. We have a helper method, sendVote(), that will log the user's vote for thier favorite game. Call it with the correct game from the list. I'm not sure how to do it. Could someone explain. Thanks.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id); // Add code here!
}
// This is a helper method to tally votes on a back-end system
public void sendVote(String game) {
VoteService.recordVote(game);
}
}
2 Answers
Ken Alger
Treehouse TeacherAlain;
Great question. Let's delve into this a bit.
Task 2
Now we can do something with the item that was tapped on. We have a helper method,
sendVote()
, that will log the user's vote for their favorite game. Call it with the correct game from the list.
If we look at what the sendVote()
method wants, we find that it needs a String
as it's input argument. We need to pass it the string representation of our favorite game, right? All of the games
are stored in the mGames
array, so for a given position, i
, we could locate that in our array with mGames[i]
. I hope that make sense thus far.
For our code challenge we need to use the sendVote()
method and pass a specific item from our array. In looking at onListItemClick()
we have an argument called position
which we can pass into our mGames[]
array to return the proper game title.
All that to say that inside our onListItemClick()
method, below the line of code we just wrote, we can write something along the lines of:
sendVote(mGames[position]);
My apologies for the rambling, but does that help at all?
Post back if you are still stuck or don't understand.
Ken
James Lowry
7,975 PointsThanks Ken and Alain!
I too was stumbling along and wouldn't have figured this out if it wasn't for this post. In the video the code suggests us writing the code to look like this:
String game = mGames[position].sendVote();
I now see why setting game to a string is redundant when all I was needing to do is call the sendVote method.
Thanks Again
alainstudio
4,493 Pointsalainstudio
4,493 PointsHi Ken, no apologies nessessary, your explanation was very useful. I had the wrong approche to the question but you made it clear. Thanks.