Welcome back.
So in this video, what we want to achieve is to actually display the data not only to look at which
is not visible to the user, but on the application itself.
So here in the history activity, we want to display a recycler view.
So we need to have an adapter for every single entry.
So let's go ahead and set up such an adapter.
Of course, it doesn't have to be as complicated because we will not need an update listener, and neither
did the listener because we are not going to update or delete the entries.
They should just be there permanently.
Of course, you can go ahead and implement the delete listener if you ever want to delete entries manually
as well, or want to have that in your application and allow this to the user.
But generally speaking, we are going to need a history adapter, which will be similar to this item
adapter, but as I said in a simplified fashion.
So let's go ahead and set it up so we can get inspired by our room database.
And you can have it open as the second screen if you want to and just try it by yourself.
And we have used multiple adapters already, but we're going to need is, of course, to have the row
itself.
So this is the item history row.
So each individual item should look like this, as we have seen at the end of the last video.
So now let's go ahead and set up such an adapter.
Therefore, I'm going to go over to my folder here and create a new Android Cortlandt file, and this
one will be the history adapter.
OK, so I'm going to call this one history adapter history adapter.
Let's go.
This will, of course, inherit from our beautiful adapter class, from recycling to you.
So recycler view dot adapter and now we need to pass into view holder.
And as we always do, we create our own view holder class inside of it.
So let's do that here as well.
I'm just going to call this one view holder with using view binding, so it will be item history row
binding, which is the name of our XML file you can see here item history row and then binding, as
always.
OK, and this will inherit from Recycler View View Holder, and we need to pass in the binding object
here as well.
Otherwise, what won't work?
But of course, I want to pass the root of the binding object, which just means this entire linear
layout basically the entire file.
OK, so just saying, OK, this is how the view holder should look like.
Now I need to get access to the individual items that we have on our item history row XML file.
So one is going to be the linear layout itself, then to text to you and this other text to you.
So this is just going to look like this El History Item Main, which uses the binding object to access
the history item main then detects your item, which gets to text your item from the binding and TV
position from the binding TV position.
OK, and now we can use our view holder that we have just created, which is inside of our history adapter
class, and it's called our view holder.
And here we need to add the brackets.
And when we do that, we of course need to implement the members as we have done many, many times so
far.
OK, now let's go ahead and implement them.
Item count.
Well, of course, our history adapter needs to get a parameter here as well, and the parameter will
just be a list of our items.
So let's call this one private vault items.
And usually we would go ahead and create a an array list or get an array list of rather complex items,
which would be objects that we would create.
But in this case, we're going to really keep it simple.
It's just going to be strings because we don't have an extra property.
How we called those entries?
OK.
So it will just be strings, which means every day is just going to be a string and and we're going
to pass that string array.
To our history adapter, because if you look at it in our history activity, you see that we're getting
this list and this list is just a list of, well, history entities, but we are not going to get it
as history entities.
But actually what we want to get is just this date.
So now you could of course say, Oh, let's use history entities here, but it really just has one property
so we can simplify it and just use the date itself, which is of that string.
So that's why we have this string array list here.
OK, so now let's go ahead and say how many items were going to return, and this will be a return item
start size, as always.
Now the uncreative you hold there, that's going to be, as always, our own view hold that we created
here this view holder here.
So return view holder with our item history binding or history row binding inflate using our layout
inflator with our parent context, as always.
OK.
And we need to pass the parent as well, as well as the attached to parent falls setting.
And that should pretty much do it.
OK, so now let's go ahead and take care of every single row.
So how should every single row be handled?
Well, first of all, let's get the date.
So I'm going to get the date, which is going to be of type string from my items.
So I need to get it at the position because I want to assign this date to my text view and I need to
get the position itself as well so we can just use the holder, which is this view holder and we can
just set it's text you position.
Text to be the position +1, so position +1 because position starts counting at zero, so if we have
an like here, you see here, if we have this entry here, it starts at zero and we don't want that
to start at zero, wanted to start at one.
So we are going to say position +1.
Now, unfortunately, position is an integer and one is an integer.
So we need to convert the result of this calculation into a string so that we can store it in the text
property of our 2x2 position, which is going to be this text you hear as you see.
OK, and now let's do the same thing with our all other you, which is our item itself, so let's set
the text of it to be the date.
And now, if we want to get this same thing that we had with our other room history example we see.
Where we had multiple different entries.
So you hear me just enter something where we had changing colors, so every second one was with a gray
background, the other second ones were with a white background.
So if it wants to have this altering background color stuff, we just need to go ahead and use the same
approach as we have done in the room database.
So let's just get inspired from it.
Therefore, we have had our item adapter with this coat here.
OK, so if position set background color, so let's do the same thing in our new project here.
Of course, to allow the linear layout is now called history.
I mean.
And the context.
Let me see get color context, so we need to have a context object or alternatively, we can even do
that actually, we can do it without passing any context to just pass the color.
So there we would need the context if we want to use a color from our colors x m l file, but we can
also just pass the color.
So just hardcoded in here, so colored dots parse color.
And either way, would have been fine, right?
You could have used the same approach there.
So I'm just going to use the white color here.
This is white and this one will be gray.
So let's do the same thing where we passed the colors.
So we set the background color to be parsed, and I think we use something like BBB here.
So BBB and it's not as many.
Values, OK, so this will give us this color effect that we have here.
This background color effect.
OK.
So I think that should be pretty much it for our history adapter.
And now let's of course, use this history adapter inside of our history activity, because that's where
we want to make sure to display it.
So let's go over to get all completed dates, because here what we want to do is want to of course,
run this for loop here, but we're going to check if all completed dates isn't empty.
We won't need this for loop because this was just for us to test it.
So we check if all completed dates list isn't empty.
And only then do we want to run some code, which means only then do we want to use our binding object
to get access to the text of history and so forth.
So binding text view history and see that we have the binding here.
We do have binding.
But I'm not sure whether we set up the history activity already, so the XML file, so let me get rid
of this line for now.
Let's go over to our layout and see if we have anything in our history.
Oh yeah, we only have the toolbar here.
That's why.
OK, so we only have the toolbar.
Of course, we need to add a proper user interface in here.
So I'm just going to use of this right here.
Let me go over it real quick.
So we still have our toolbar.
So use a constraint layout.
And generally speaking, I want to have this here.
OK, so this is what I want to have.
This layout, you can see, have the toolbar at the top, then we have our text view which says exercise
completed or exercises completed or whatever.
Then we have our recycler view and then we have this text view, which says no data available.
So this text view should be visible by default.
And this recycled view, as well as this text, you should only be visible if we have any history data
to be displayed.
So let's look at it.
We have this text view here, which is going to be at the bottom of our toolbar history activity.
Then we call it Text your history.
Then we have the recycle view, which will be underneath text view history.
It will be gone by default.
So this is going to be the default visibility.
And then we have this text you at the bottom, which is just saying that we have no data available,
so we haven't finished exercises yet.
We could, of course, say something like do some exercise now or something like that to motivate someone
who gets to this mystery screen.
And this should be also underneath the toolbar history activity, but it should be at the same time
to the bottom of the parent.
So it should be basically dragged towards the top as well as towards the bottom, but then be in the
center.
So that's what we do with this gravity center.
So we're just saying OK, centered is no data available thing and constrain it towards the toolbar as
well as towards the bottom of the screen.
OK, so now let's use this activity history as XML file inside of our history activity Caity file because
now we can use this binding object to access the properties in our XML file, so to access the text.
So let's do that.
Let's access our texture history here.
Which will be binding the TV history.
And I'm going to set that to be visible.
So visibility.
Well, view that visible because if we have any data, we want to make it visible.
If we don't have data, we want to make it invisible.
And the same goes for our respective of you.
So we want to make that one visible as well.
So viewed that visible.
And we want to make our text viewed that says we don't have any data invisible.
So text, you know, data available visibility should be said to view thought invisible.
OK, so now we can set the respect of you to be set up and let me actually before we do that.
Let me add to Outlook Block.
This will make it a little or a little easier for you to follow, I believe.
So here in the other case, we're going to do the opposite.
So we're going to make this stuff here, invisible or gone.
And this text view that says no data available.
This one here visible.
So if we have no data displayed, otherwise don't display.
And now we can set up our recycle view.
So here our recycle view needs to have a layout manager.
So let me get access to this recycle view and set up its layout manager property to be a linear layout
manager because I just want to have items to be on top of each other.
Now, I cannot just use this here because we are inside of a lifecycle scope, so we're in a closure.
So this is code that if we use this, then it will believe it's inside of the doll or something like
that.
So here we can just make sure that we use to history, activity context.
So here let me see that I read it correctly.
No, I did not.
Activity like this.
OK.
So now that we have our respect of you, we need to set up the dates for it.
So let's get the dates which will be in the list of strings.
OK, and then let's get the dates from our all completed dates list.
So it's this one here that we get from our fetching all data, so we called this method that will give
us all the data that we have in the history table.
And now we can use it because this is the data that we get right.
I'll complete it dates.
And if it's not empty, we're going through all of them and we're going to create a date out of every
single entry.
And now we can add every single date to our dates ArrayList that we have here, which is just an array
list of strings and can assign it to our respective you after.
Because if you look at our respective adapter, it requires exactly that.
It requires an array list of strings.
So let's add the dates in here.
And I think data itself is not going to be it, we need to use data to date because the date will be
a history entity, which is not a string.
But the history entity has an entity called Let's jump over to the history entity to see that history
entity.
It has a string property that is called date.
OK, so we get that date.
Property by getting data data.
OK, so now that we have all of the data, we can create our history adaptor because it needed to have
our.
Dates which we now can assign here.
So let's use the dates, and now we can assign this history adapter to our recycler view adapter property,
as always.
So always the same approach, right?
You need to set up your adapter.
You need to prepared a list that you want to use, and usually it's in radius.
You need to then go ahead and set up the list.
Fill it with data.
In our case, we get the data from our database and then use this data inside of the adapter or pass
it to the adapter and then use this adapter that you set up as the adapter for your recycler.
View that you have prepared.
So here we can just use our history adapter now, and this should pretty much do it.
So let's go ahead and testers and see if we have any exercises that we have finished inside of our history,
screen or history activity.
And then we can also just quickly finish some more exercises.
So let's go over to our history.
We see we have finished this exercise at 10, 23 36.
So exercises completed.
And even though you should, and as here and now, if we go through it once again, we will then see
that this new entry has been added also.
All right.
So this will make it white, I think, from the background.
So the first will be gray than white, and the third one would be gray again because that's what we
have set up inside of our history adapter on Bind View Holder.
So here we're iterating the colors, so we finish the exercise.
Now let's jump over to the history and we see the new history has been added.
So that's pretty much it.
So now our application is pretty much complete now.
You could, of course, make it prettier.
Make sure that your main color is also this greenish color or, well, basically just lend the application
to whatever you need it for.
So I hope you enjoyed this chapter, and I hope you can use this to build your own specific apps that
have to do either with timers or with the recycler views or with permanent data storage and so forth.
So thanks a lot.
And see you in the next video.