Creating the Adapter12:17 with Ben Deitch
In this video I create a custom adapter for my ListView and then set that adapter as MainActivity's ListAdapter.
Now that we have our holes object, 0:00 we can use it in a list adapter which we're going to need for our list view. 0:02 So, let's create it. 0:07 I'm going to call my adapter ListAdapter. 0:09 And it will extend BaseAdapter. 0:14 Then I'll use Alt+Enter to implement methods. 0:20 From previous adapters, I know that I'm going to need a context at some point. 0:27 Also, for this specific adapter, I'm going to need the holes. 0:32 So let's create a constructor to get both of those. 0:36 Then I'm going to use the n prefix to let Android Studio know that I'd 0:53 like fields to be created. 0:58 Then i'll use Alt+Enter to create the fields. 1:05 And delete this space because I don't like it. 1:14 All right. 1:18 Now that we have access to our holes, we can update some of these functions. 1:19 For example, get count, should now return mholes.length, 1:25 get item at position should return mholes of index position. 1:31 getItemId. 1:40 We won't implement. 1:40 And then getView which is the tricky one. 1:44 Before we get into getView, recall that we're going to need a ViewHolder. 1:47 So let's type private static class 1:52 ViewHolder and now we have our view holder class. 1:57 The ViewHolder class just holds the views, so 2:04 all it needs is variables for each of the views we'd like it to hold. 2:07 In this case, it's each of the views from out listitem.xml, so 2:12 we're going to need one TextView, two TextViews, one, two buttons. 2:16 So two TextViews and two buttons. 2:23 Go back to list adapter, and let's make a TextView. 2:26 And It will be called HoleLabel 2:31 cause that's the first TextView and another TextView called strokeCount 2:36 and then the two buttons, removeStrokeButton then 2:45 Alt+Enter to import the button, and addStrokeButton. 2:49 All right, now that we have our view holder, 2:54 we can get back into getView. 2:59 The first thing I'm going to do is declare a ViewHolder variable, and 3:06 I'm going to declare this final. 3:09 The upcoming if block, 3:16 which is going to be if convertView is null, 3:19 and else needs to be able to handle the view holder. 3:24 So we'll set it here, or here. 3:30 So, if convert view is null, then we needed to set it to a view. 3:34 Which will be layout inflator.from, and 3:42 this is where we needed the context then .inflate. 3:45 And we wanna send it our list_item and a null for the theme. 3:53 Now that we have our convertView, we need to create our ViewHolder and 3:59 add it to that convert view as a tag, so holder = new 4:04 ViewHolder() and then we need to set each of the properties of our view holder so 4:08 holder.holeLabel = (TextView). 4:15 And we want to textView from the convertView, so 4:23 we'll do convertView.findViewById(R.Id.holeLabel); 4:28 Looks good, then I'm going to use the Cmd or 4:34 Ctrl+D shortcut to duplicate this line three times. 4:38 Then I'm going to copy this up to here, 4:45 This to here, addStrokeButton is going to go up to here. 4:51 These two need to be buttons, And we'll need to change these ids as well. 4:56 All right, now that we've done that, 5:13 all that's left is to set the convertViews tag to our holder. 5:16 So we'll set the tag to our holder object. 5:23 So on the flipside, if convertView isn't null, 5:33 all we need to do is get the holder from our convertView. 5:38 So holder equals, 5:42 we're going to cast it as a ViewHolder, 5:45 and convertView.getTag. 5:51 All right? 5:55 Now that we definitely have our holder variable, regardless of if convertView is 5:57 null or not, we can use it to set the properties of our list_item.xml. 6:01 So holder.holeLabel.setText, and 6:10 we want to set it to (mHoles[position]) 6:16 to get the hole that we're currently 6:22 getting the view for .getLabel. 6:27 And we want to do something similar for the strokeCount. 6:35 SetText(mHoles[position].getStrokeCount, perfect. 6:40 Then we also want to set the onclicklisteners for 6:50 our buttons to make sure that they are updating the correct view. 6:53 .setonclickListener, then we'll type new, capital O, 7:00 tab and that willl make that all automatically appear. 7:05 And when we click on the remove stroke button, 7:11 what we want to happen is we want the stroke to decrement by one. 7:14 So let's create a updated new stroke count variable to handle that, int 7:18 updatedStrokeCount and we'll set it equal to 7:24 mholes position dot get stroke count minus one. 7:31 There's a pretty clear bug here that we need to handle and 7:39 that's what happens when they're already at zero strokes and we put minus one. 7:42 We'd show a minus one on the screen, which is impossible in golf, so 7:48 we should handle that. 7:53 So if updated stroke count is less than zero, updated stroke count should be zero. 7:56 And you can omit brackets for your if statements if it's all on one line, and 8:05 there's only one statement. 8:08 Now that we have our updated stroke count, which is definitely valid, 8:11 we can use it to set the text. 8:15 So we'll start by setting this hole's stroke count To the updatedStrokeCount, 8:22 and then setting the strokeCount on the screen to the new strokeCount as well. 8:30 And this reminds me that we'll want to add this plus empty string right here as well. 8:46 When we pass an integer to the setText function, it's going to look for 8:55 a resource with that integer. 8:59 But we don't want it to look for a resource. 9:02 We're passing in what we want it to take as a string. 9:04 So to make this a string, we'll just add an empty string to it. 9:08 All right. 9:11 Now let's do the add stroke button. 9:16 Set on click listener, new, capital O, tab. 9:22 And then this is very similar to before. 9:29 Updated stroke count equals mHoles position. 9:33 Plus one this time. 9:39 mHoles position.getstrokecount. 9:44 And we don't need to any special check for 9:49 this stroke count because your stroke can go as high as it can. 9:51 So, mholes, position, set the stroke count for the whole object here. 9:54 And then we'll set the text on the screen as well 10:04 Looks good. 10:14 Now the last thing we need to do is change this return null. 10:17 We should be returning a view which Technically null works for that, but 10:23 we don't wanna return a null view for our whole list, we wouldn't see anything. 10:28 So let's change this to convert view. 10:32 All right, I think we're done with the list adapter class. 10:37 The last thing we need to do with our list adapter is set it in main activity. 10:40 We'll want to set it, after we initialize the holes, 10:45 because we need to pass them into the constructor. 10:48 We'll also want our list adapter to be a field. 10:52 So I'll start it with an m prefix, to let Android studio generate that for me. 10:57 For the context we can either pass N, get application context or just this, 11:06 which is main activity.Both are context so either works. 11:11 And then we need the holes variable. 11:17 And Alt+Enter to create our list adapter field. 11:23 And then we just need to set the list adapter to m list adapter. 11:28 All right, that looks good. 11:34 Let's see what happens when we run it. 11:36 All right, here we've got all our holes, and 11:39 when we hit the plus button, the score goes up. 11:41 When we hit the minus button, it goes down. 11:47 Let's give him a hole in one on Hole 1. 11:52 And if we scroll to the bottom, 18 holes, just like we would expect. 11:55 But if we leave the app and come back to it, the scores are all zero. 12:03 It completely forgot what the score was. 12:10 How useless. 12:12 Let's fix that. 12:14
You need to sign up for Treehouse in order to download course files.Sign up