Tuesday, 13 September 2022

How to use Block Manager Part 2

Diese Website verwendet Cookies von Google, um Dienste anzubieten und Zugriffe zu analysieren. Deine IP-Adresse und dein User-Agent werden zusammen mit Messwerten zur Leistung und Sicherheit für Google freigegeben. So können Nutzungsstatistiken generiert, Missbrauchsfälle erkannt und behoben und die Qualität des Dienstes gewährleistet werden.

Look what happened at Pat I: 
* You know where to find Block Manager, * how to create a new palette and a new block. 
* How to set the properties, 
* how to define the mask and parameter * and to set placeholder for the parameter in the code. 
* At last we tested the new blocks in a comment app.

This part shows to copy java code to the block and to replace the parameter with placeholders. 
For this reason we take a tutorial of 
Sketchwarehelp.com and take the code for the Asd- blocks for to generate the new blocks.

I have made a address list with Listview listview1 on Main activity.
Set the custom view of listview1 to listview1.
In the custom view i added three linear horizonal.
Inside linear1 i add two TextView for first name and name.
Inside linear2 i add one TextView for street and 
inside linear3 i add two TextView for postal and town.
Set padding of all three linear to 0 for to look it smaller.

In onCreate event add 
* Map map 
* ListMap maplist
Add a new mapp and put five items to it with even keys: first name, name, street, postal, town. Then add the map to the maplist. 
You can copy this as for how much entries you want and fill the values. 
At the end set custom view data of listview1 to maplist and refresh the list.
Close the onCreate event.
Add the onBindCustomView event
Set for every TextView the text to the according list item with specific key at position.
You can now run the app first time to see how's looking

In the tutorial you now create a more block. But instead we now create the sort block.
Start Block Manager and add a new palette. Title 'Sort ListMap' and give it a color. 

Open the palette and add new block.
Set properties to: 
name: sort ListMap
type: regular (r)
color: as you want 
 Now write into the the mask "sort " and
click the parameter listMap
Cntinue writing " according to key " and click parameter string.
Now go to the tutorial and copy the red written code.
The first parameter stands for the maplist. You must now search the maplist 
'_mylist' in the code and replace it with the placeholder %1$s.
The second parameter stands for the key string. You must now search every '_key'
in the code..
 ..and replace it with %2$s.

💡You must do that for every parameter you add in the mask. For the third parameter search every of it in the code and replace it with %3$s, for the fourth parameter replace it with %4$s and so on. But here we have only two parameter.
That's it. Click save to create the block.

On MainActivity add now a Spinner spinner1 over the ListView. Open the onCreate event and add a ListSring stringlist. Add now for the keys a entry to stringlist and set it's data to 
spinner1.
Leave the onCreate event and add for the spinner the onItemSelected event.
Open this event.
Add a if block with condition 'position= 0'
Inside refresh the ListView.Its time to use your block. Open the block list and find it.
Drop it above the refresh blocks inside the if block. Set the ListMap to maplist and the key string to the first key: 'first name'Copy this if-block three times and make changes to the number and key strings.
Now if you click the spinner, you can sort it by first name, name, postal and town.
You can now start the app. 👍
📝 In the tutorial of Sketchwarehelp.com there is also the code for sort reverse. You can also create a block in the same palette, name it sort reverse add the listMap and key string as parameter and paste the sort reverse code and replace the parameter with the placeholder like descripted below. 
You can then add three sort reverse items for every key in the list for the spinner. 

No comments:

Post a Comment