Hey there.
Well let's now apply our knowledge all the hash map in our music shop app.
Let's create a hash map in which we'll be connected their name or the product and its price.
Let's create a variable of type hash map and name it.
Goods map then in method on create a sign.
These variable goods map from the word goods.
A new object of type hash map
and now field this structure includes a map put.
Since we will add music stores instruments first I'll add guitar and price of five hundred dollars.
Void
then copy this line and paste and here will change the drums.
We'll make the price 1000 or 500.
And here we'll put the price keyboard and price.
Let it be you 1000.
So now we need to select the product.
The application should automatically read product price by quantity and display this in these text for
you.
Let's look at the name of these sticks to you in our file activity mean you can go to the desired tab
click on these text to view and we see here it's name I.D. price sticks to you.
Now let's go back to main activity in order to implement a reaction to selecting elements in the piano.
We need to implement the in the class main activity interface on item selected listen up their Ts.
Our class will listen to events when the user selects an element in the spinner and will respond to
the C word.
The interface is implemented in this way.
We write implements and here we are right on item.
Selected listener
and as you can see Andrew is to emphasize this line in the red.
This is because it is written here.
The class must be either abstract or implement methods of this interface on item selected alerts.
Click on the red lamp and click on implements methods click or gate.
And as you can see androids to do it automatically as two methods to our activity.
These methods are all an item selected and on nothing selected in the on item selected method.
We write the code that must be produced which must be executed when some element is selected.
Let's write our code here.
Let's first create a variable of type 3.
Name it good simile their name or their products
next in the item selected method.
We assign a good name.
These are variable.
Let's write spinner get selected item to string.
That is the value that is currently in the spinner selected value.
Next create a variable of time.
Double call it price
add in the on item selected method.
We will assign this variable price
their value from our goods man.
We're the key goods any
that is we will get the price associated with this name.
Their line is underlined in red.
This is because in our map there can be elements of any type.
And since Java is strongly typed language it requires that a variable of type double price variable
be assigned all the value of double.
There is a way out of this situation.
It is called type cast.
Since we know that in our map the numerical values for the time double.
We can write here in brackets.
Double and no we show that we get their value from our hash map and we bring it to the value of the
type.
Double and assign it to a viable over the type double next declare a variable of type.
You name it price ticks through.
Just like a normal layout
and bind it
to our text to with I d prior 6 2 in all out and then set the text to these text you with method set
text and here we specify double course first because we need to set the line 3 Sorry and plus we want
their number to be displayed here.
Now let's write.
Quantity our variable multiplied by the price of the product that is selected in this piano this is
price and now let's try a Toronto application.
Let's choose the quantity one.
Now we will choose an element for example a guitar.
So for some reason nothing was displayed.
Well let's see what's the matter.
Yeah.
There is no reaction to our speed up.
This happened because when creating the spinner you need to send the listener for it.
Set on item selected listener and indicate these here indicate our activity.
We implemented in our tiered activity.
This interface which allows these main activity clubs to listen to selection events to be in the spinner.
And now we need this listener of events our activity.
These two install to the spinner so that our class listens to the life or the spinner.
Let's front again
and so our application was stopped.
Let's see what's the matter.
Huh.
Here it is a written class cast exception type Integer cannot be cast to type double and play a number
of main activity seventy two online 72 hour variable price of a ratio variable of type double is assigned
a value extracted from the hash map and we are trying to convert this value value to the time double
but Android's sudo swears here is that an integer cannot be cast to the cause double.
This happened because we place the value 500 in the hash map and by default it it is set us integer
so that it is set as double.
You need to write it in the form of a decimal number that is with a dot.
Let's write all the values in the form of this small numbers.
And now in our hash map the strings double balance will be linked.
Let's write the application again
let's clique plus bottom select then number one and click the drums.
And as you see wonderful we got a prize of one thousand five hundred drums.
Let's try to increase the number by 2.
Again try to press the drums so does not exceed.
Let's hit the guitar.
And so one guitar costs five hundred.
Two guitars.
One thousand dollars.
That's right.
And let's try the drums to drum kit course.
Two thousand dollars.
That's right.
But our application does not work correctly.
It is advisable to make sure that when you press the buttons when the number changes the amount of the
order immediately changes.
Let's try to implement these limits for this at a increased quantity and decrease quantity in our methods.
Add these two lines where we set new values in the text to you when that quantity and product change
will restart the application.
Well that's cool.
We immediately display the amount of five hundred dollars since we immediately selected a guitar.
The value for one guitar will be five hundred four to one thousand for three one thousand five hundred.
We do the amount also decreases.
Let's try out the other elements.
Drums.
So two drums says course.
Three thousand one course.
One thousand five hundred three equals four thousand five hundred.
Keyboard three keyboards course.
Three thousand two course.
Two thousand dollars.
Everything is awesome.
We have succeeded.
And let's now implement in the next task the image change.
When the product is changed so that when we select the drum kit the drum kit is displayed here.
If we select the keyboard to display the keyboard here.