4 hours 3 minutes
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.
What you do is to observe the result.
So let's get to work
in our own click
we want when we click
we get the body from the D. Text and the spinner
and then inserted. So here it is
we get the funding from the tip text
from this pena.
So we create a new deputy able object assigned the item to the name
to the integer value of the amount.
Then we see
the view model dot in such new item
they won't pass in this DB table.
So when we say in some new item on the View model
it's a contrary view model.
In some new item
this will in turn call the item repository. The item reposition will call our item
running in the background
until the database to insult it.
It might seem like a long route. But then when you have big applications this architecture will help you have more maintainable applications.
So now every time we click the button and we have our name and amount it will start it into the database.
We will say the V. Model thought get items
now get items, returns a life data
so we can observe it.
So passing this representing the lifecycle owner
2nd argument is a new observer.
If you're familiar with
lambda expressions and functional interfaces in java
of course I can replace this with lambda expression.
I click in this as he replaced with lambda.
So we call the set data of the item adapter,
notify that assets changed.
What this will do is to tell the adapter
its data has been updated. So that issue update the secular view because when you say set data
it will assign new values to this list.
When you now go on to call
notify that asset changed. You're telling this adapter that hey
this list has new items so
run the process again of you know
displaying the updated view to the user
now because this is observed
any time the query of the get items which is the items in the database. Once any item is added or removed
this would be called
and then this process will be run again.
Once the application is started this would be called
he called here was acute
and then once these changes it will be called again.
So here is for the account we get item count, observe it
now if anything changes we say text account dot set text less islam the expression
so it will be called initially
When the database is empty text account of zero
once anything is added to the database of course the car to also change so to update. The Txt counts text
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
if I say add
Did you see that beans quantity four
immediately the amount of items was implemented.
If I come here and I say
what's missing now is any time I click on this deletes nothing happens. So let's fix that
to put a click listener on delete icon. Let's go to the adapter.
So interview holder.
on click listener.
Oh quick we'll get the adapted position
then get the current deputy able item using the position.
Now we get our item repository instance and then say item repository dot delete.
After that we can display a toast deleted
so we will not tell image view to set the quick listener
to this this million of implemented in this class. And on click
this is a method reference.
You can as well
leaving us this.
So let's run this application
Click deletes the pizza delivery to know posted now items is one
there we can have what you like. Uh let's say
Meet you just want to buy meat. Just meet nothing much. three ad
you can see the real time updates.
I decide to delete everything we have zero.
The source code will be available for you to actually look at and really study.
I hope you've enjoyed building one of your first applications.