9.5 Shopping App Part 3

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
4 hours 3 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:02
The next thing we need to do is to get and work with a View Model anytime you call the View model and the results you're getting is a life data.
00:13
What you do is to observe the result.
00:17
So let's get to work
00:19
in our own click
00:21
we want when we click
00:23
we get the body from the D. Text and the spinner
00:27
and then inserted. So here it is
00:30
we get the funding from the tip text
00:34
from this pena.
00:36
So we create a new deputy able object assigned the item to the name
00:42
quantity
00:45
to the integer value of the amount.
00:48
Then we see
00:50
the view model dot in such new item
00:54
they won't pass in this DB table.
00:58
So when we say in some new item on the View model
01:03
it's a contrary view model.
01:07
In some new item
01:08
this will in turn call the item repository. The item reposition will call our item
01:19
running in the background
01:21
until the database to insult it.
01:25
It might seem like a long route. But then when you have big applications this architecture will help you have more maintainable applications.
01:37
So now every time we click the button and we have our name and amount it will start it into the database.
01:46
We will say the V. Model thought get items
01:51
now get items, returns a life data
01:56
of list
02:00
so we can observe it.
02:04
So passing this representing the lifecycle owner
02:07
this activity.
02:09
And then
02:10
2nd argument is a new observer.
02:15
If you're familiar with
02:16
lambda expressions and functional interfaces in java
02:22
of course I can replace this with lambda expression.
02:24
I click in this as he replaced with lambda.
02:30
So we call the set data of the item adapter,
02:34
notify that assets changed.
02:37
What this will do is to tell the adapter
02:40
but
02:42
its data has been updated. So that issue update the secular view because when you say set data
02:49
it will assign new values to this list.
02:53
When you now go on to call
02:54
notify that asset changed. You're telling this adapter that hey
03:00
this list has new items so
03:05
run the process again of you know
03:07
displaying the updated view to the user
03:10
now because this is observed
03:14
any time the query of the get items which is the items in the database. Once any item is added or removed
03:23
this would be called
03:24
and then this process will be run again.
03:29
Once the application is started this would be called
03:31
he called here was acute
03:34
and then once these changes it will be called again.
03:38
So here is for the account we get item count, observe it
03:44
now if anything changes we say text account dot set text less islam the expression
03:52
so it will be called initially
03:53
When the database is empty text account of zero
03:58
once anything is added to the database of course the car to also change so to update. The Txt counts text
04:10
So if I come here and I write beans, I love beans. So let's see a bag of beans or something like that. And I say four
04:18
if I say add
04:19
Did you see that beans quantity four
04:24
immediately the amount of items was implemented.
04:28
If I come here and I say
04:30
mhm.
04:32
Is a
04:35
mm.
04:38
Yeah
04:40
yeah
04:42
what's missing now is any time I click on this deletes nothing happens. So let's fix that
04:49
to put a click listener on delete icon. Let's go to the adapter.
04:57
So interview holder.
04:59
Well put
05:00
implements
05:04
on click listener.
05:06
Let's implement.
05:09
Yeah.
05:10
Oh quick we'll get the adapted position
05:14
then get the current deputy able item using the position.
05:18
Now we get our item repository instance and then say item repository dot delete.
05:26
After that we can display a toast deleted
05:30
so we will not tell image view to set the quick listener
05:33
to this this million of implemented in this class. And on click
05:41
this is a method reference.
05:43
You can as well
05:46
mm
05:46
leaving us this.
05:51
So let's run this application
05:58
Click deletes the pizza delivery to know posted now items is one
06:03
there we can have what you like. Uh let's say
06:10
Meet you just want to buy meat. Just meet nothing much. three ad
06:15
you can see the real time updates.
06:20
I decide to delete everything we have zero.
06:26
The source code will be available for you to actually look at and really study.
06:32
I hope you've enjoyed building one of your first applications.
Up Next