1
00:00:04,200 --> 00:00:05,200
Welcome back.

2
00:00:05,600 --> 00:00:07,800
We've got the layouts for our reports.

3
00:00:08,100 --> 00:00:12,200
Next we need to create an adapter to provide the data for the RecyclerView.

4
00:00:13,000 --> 00:00:15,800
Most of this will be very similar to our existing

5
00:00:15,800 --> 00:00:18,000
cursor RecyclerView adapter

6
00:00:18,000 --> 00:00:20,300
that we created back in section 10.

7
00:00:21,100 --> 00:00:24,300
This one won't be responding to taps, so it's a bit simpler.

8
00:00:25,100 --> 00:00:28,700
It does have to perform some date formatting, but other than that,

9
00:00:28,950 --> 00:00:30,150
it's a basic adapter.

10
00:00:31,350 --> 00:00:34,550
If you're not sure how an adapter works with a RecyclerView,

11
00:00:34,550 --> 00:00:36,910
it's worth watching those earlier videos again.

12
00:00:38,410 --> 00:00:42,310
Okay. Right click the package name and create

13
00:00:42,310 --> 00:00:44,810
new Kotlin File/Class

14
00:00:55,310 --> 00:00:58,970
and call it DurationsRVAdapter.

15
00:01:04,269 --> 00:01:06,870
The name of the other adapter is a bit long

16
00:01:06,870 --> 00:01:11,230
because it wanted to warn you about the problems you could get with logging tags.

17
00:01:11,730 --> 00:01:16,330
I'll keep this one short by using RV rather than RecyclerView.

18
00:01:16,630 --> 00:01:18,730
It's useful to have that in the name

19
00:01:18,730 --> 00:01:21,030
because there are different types of adapters.

20
00:01:21,430 --> 00:01:25,430
Change the kind to class to save a bit of typing.

21
00:01:26,830 --> 00:01:27,830
Then click okay.

22
00:01:29,430 --> 00:01:32,630
Our class will extend RecyclerView.adapter,

23
00:01:33,030 --> 00:01:35,330
and we'll need to provide a view holder.

24
00:01:35,630 --> 00:01:38,830
We've already used the layout container interface

25
00:01:38,830 --> 00:01:42,430
to avoid having to call find view by ID.

26
00:01:59,090 --> 00:02:02,190
Our adapter will need to be a sub class

27
00:02:02,190 --> 00:02:04,790
of RecyclerView.adapter class,

28
00:02:05,840 --> 00:02:09,139
and we'll need to provide it with the context and the cursor.

29
00:02:09,539 --> 00:02:13,340
This is the same as we did for the cursor RecyclerView adapter class.

30
00:02:30,140 --> 00:02:32,740
I'll generate the functions we need to override

31
00:02:32,740 --> 00:02:35,240
just after the class declaration.

32
00:02:52,340 --> 00:02:55,800
The code for the GetItemCount function is straightforward.

33
00:02:56,200 --> 00:03:00,200
We're not doing anything fancy here. There are no records to display.

34
00:03:00,200 --> 00:03:02,460
The RecyclerView will just be blank.

35
00:03:16,260 --> 00:03:18,760
We also need the SwapCursor function,

36
00:03:19,360 --> 00:03:22,960
and I can copy that from our cursory RecyclerView.adapter class.

37
00:03:23,960 --> 00:03:25,950
It appears near the end of that class.

38
00:03:26,850 --> 00:03:30,150
I'll paste it just after the functions that we've generated.

39
00:03:49,150 --> 00:03:52,150
The code for this function is identical to the other adapter.

40
00:03:52,650 --> 00:03:56,640
In fact, you'll probably use the same code or something very similar

41
00:03:56,640 --> 00:03:59,640
in all your RecyclerView.adapters that use a cursor.

42
00:04:00,040 --> 00:04:03,540
The oncreateviewholder function is also very simple.

43
00:04:03,840 --> 00:04:06,140
It just inflates a view from our layout

44
00:04:06,540 --> 00:04:10,340
and uses it to create a ViewHolder then returns the ViewHolder.

45
00:04:27,740 --> 00:04:30,040
We're going to do some basic date formatting,

46
00:04:30,040 --> 00:04:33,840
so the app can display dates in a format that our users will expect.

47
00:04:34,740 --> 00:04:38,040
Dates in the United States appear with the month first,

48
00:04:38,040 --> 00:04:41,040
while in Europe and Australia amongst other places,

49
00:04:41,040 --> 00:04:43,040
the day appears before the month.

50
00:04:43,700 --> 00:04:46,960
There are also differences in the date separator that's used.

51
00:04:47,660 --> 00:04:50,660
Some countries use a slash, others use a dot.

52
00:04:51,020 --> 00:04:53,620
We can use a date format object for that

53
00:04:53,620 --> 00:04:55,820
to handle the formatting of the dates for us.

54
00:05:04,320 --> 00:05:06,020
When you're prompted for the import,

55
00:05:06,020 --> 00:05:09,320
make sure you choose the one from android text format.

56
00:05:12,120 --> 00:05:14,480
I'm going to add another function to this adapter.

57
00:05:15,140 --> 00:05:19,240
We're going to be using durations, which we want to format in hours,

58
00:05:19,240 --> 00:05:20,600
minutes and seconds.

59
00:05:21,490 --> 00:05:25,890
Using a colon to separate the three parts should be universally understood

60
00:05:26,140 --> 00:05:29,140
but we can't use a standard date formatter to do that.

61
00:05:29,540 --> 00:05:32,240
The reason is that the current duration for a task

62
00:05:32,240 --> 00:05:34,010
could be more than 24 hours.

63
00:05:35,010 --> 00:05:37,510
We're displaying the total duration for a week.

64
00:05:38,010 --> 00:05:40,810
And in a week, our users could easily spend

65
00:05:40,810 --> 00:05:42,810
more than 24 hours on a task. The

66
00:05:43,310 --> 00:05:45,810
normal date, time formatting functions

67
00:05:45,810 --> 00:05:49,810
will either attempt to convert that to days, hours, minutes and seconds

68
00:05:50,310 --> 00:05:54,910
or give an incorrect result when attempting to represent it as a time of day.

69
00:05:55,460 --> 00:05:57,760
If we try to use normal date formatting,

70
00:05:58,120 --> 00:05:59,720
it's going to go horribly wrong.

71
00:06:00,520 --> 00:06:04,510
Date classes can't handle a time with more than 24 hours.

72
00:06:05,170 --> 00:06:08,670
I'll add the function after onbindviewholder

73
00:06:09,030 --> 00:06:11,230
and call it formatDuration.

74
00:06:44,730 --> 00:06:46,230
There's not a lot to say about that.

75
00:06:46,830 --> 00:06:50,130
It takes number of seconds, calculates the hours,

76
00:06:50,130 --> 00:06:54,230
minutes and seconds remaining and returns them as a formatted string.

77
00:06:55,030 --> 00:06:56,930
We use a default locale,

78
00:06:56,930 --> 00:07:00,290
the one that the user has specified in the device settings.

79
00:07:00,990 --> 00:07:03,990
Some languages, such as Thai for example,

80
00:07:03,990 --> 00:07:06,190
use different characters for the digits,

81
00:07:06,550 --> 00:07:09,750
and the user may have specified that these are the ones that they want.

82
00:07:10,300 --> 00:07:15,290
All right. The last function we need to write is onbindviewholder.

83
00:07:15,950 --> 00:07:19,610
Once again, it's fairly standard code except for a bit of date formatting.

84
00:07:20,110 --> 00:07:22,710
I'll paste the code rather than typing it.

85
00:07:30,210 --> 00:07:33,210
You can pause the video here with the code on the screen,

86
00:07:33,460 --> 00:07:35,660
so that you can catch up with the code.

87
00:07:40,660 --> 00:07:42,020
There's nothing clever in there.

88
00:07:42,620 --> 00:07:45,120
We use a local variable for the cursor

89
00:07:45,120 --> 00:07:49,220
to allow Kotlin to perform smart casts after a null check.

90
00:07:49,920 --> 00:07:52,820
Most of the code just retrieves the data from the cursor.

91
00:07:53,620 --> 00:07:56,920
Note that we can refer to the ViewHolder fields directly

92
00:07:56,920 --> 00:08:00,320
because it implements the layout container interface,

93
00:08:00,870 --> 00:08:04,430
allowing the widgets to be accessed via the synthetic import

94
00:08:04,430 --> 00:08:06,630
instead of find view by ID.

95
00:08:07,230 --> 00:08:09,530
I'll discuss that date format briefly.

96
00:08:10,080 --> 00:08:12,080
If you're dealing with dates and times,

97
00:08:12,080 --> 00:08:14,780
you want to format them in a way that the user expects.

98
00:08:16,280 --> 00:08:21,180
Up on line 25, we created an instance of the date format class.

99
00:08:21,580 --> 00:08:25,180
The getDateFormat function is past a context,

100
00:08:25,540 --> 00:08:28,640
and it can use that to work out which locale it should use

101
00:08:28,640 --> 00:08:29,940
when formatting dates.

102
00:08:30,740 --> 00:08:32,340
We'll test that later

103
00:08:32,340 --> 00:08:35,440
and make sure that the dates do appear as they should

104
00:08:35,440 --> 00:08:36,940
in different parts of the world.

105
00:08:37,440 --> 00:08:40,640
Use ctrl Q or ctrl J on a mac

106
00:08:41,039 --> 00:08:45,040
to call up the documentation for that getDateFormat function.

107
00:08:50,240 --> 00:08:53,600
If you press control Q or control J a second time,

108
00:08:54,000 --> 00:08:57,200
your documentation will be locked to the right hand edge of the window,

109
00:08:57,200 --> 00:08:58,700
which can be more convenient.

110
00:09:01,900 --> 00:09:04,400
GetDateFormat returns an object

111
00:09:04,400 --> 00:09:07,500
that can format a date according to the current locale

112
00:09:08,160 --> 00:09:11,860
that means we'll get a different format on an American phone

113
00:09:11,860 --> 00:09:13,560
then I'll get on my Australian one.

114
00:09:14,440 --> 00:09:18,740
All the emulators are set to the United States locale by default.

115
00:09:19,340 --> 00:09:22,340
But we'll change that later to make sure everything's working.

116
00:09:23,340 --> 00:09:27,340
The other thing to check is that we don't try to access the description

117
00:09:27,340 --> 00:09:29,340
if the device is in portrait.

118
00:09:29,740 --> 00:09:33,740
We didn't include a texture for the description in the portrait layout.

119
00:09:34,740 --> 00:09:36,340
Rather than test for null,

120
00:09:36,840 --> 00:09:39,840
I've used the Kotlin safe call operator,

121
00:09:40,540 --> 00:09:43,040
that's tidier than the equivalent java.

122
00:09:43,590 --> 00:09:48,190
But I like to include a comment, a question mark is quite easy to miss

123
00:09:48,190 --> 00:09:51,490
when you're reading the code. That's our adapter finished.

124
00:09:52,150 --> 00:09:56,250
We've had to do a bit of processing on the data but all the hard work,

125
00:09:56,250 --> 00:09:59,050
totaling the daily durations for each task

126
00:09:59,050 --> 00:10:01,650
was performed when we created the view that we're using.

127
00:10:02,650 --> 00:10:06,650
In the next video, we'll create an activity to display the data

128
00:10:06,650 --> 00:10:08,250
so we can view our reports.

129
00:10:08,750 --> 00:10:10,450
The code will be temporary

130
00:10:10,450 --> 00:10:14,250
because we'll be using a view model to provide the data to the adapter.

131
00:10:14,750 --> 00:10:17,950
That will prevent things like having to re-query the database

132
00:10:17,950 --> 00:10:19,550
if the device is rotated.

133
00:10:20,350 --> 00:10:23,950
If we wait until I've written the view model before testing our adapter code,

134
00:10:24,500 --> 00:10:27,100
it can be harder to work out where any problems are.

135
00:10:27,600 --> 00:10:30,800
Also note that I haven't included any logging

136
00:10:30,800 --> 00:10:33,160
in this durations RV adapter code.

137
00:10:33,710 --> 00:10:36,710
You know how to do that now. And if you get any problems,

138
00:10:36,710 --> 00:10:39,310
adding logging will be your first step to diagnosing them.

139
00:10:39,710 --> 00:10:41,710
All right. I'll see you in the next video.

