AppInventor – Create Dynamic First List for Listpicker

Following on from my recent post on Dynamic Lists for Listpickers, I ventured a bit further.

Well I now have a rough version (and it is ugly!) that creates a dynamic list for the first list in a nested listpicker. This assumes a list of names or words in the master list, and that these are sorted alphabetically. The issue with creating such a thing is that, say for names, there are 26 letters that names could start with, but that the distribution of names is very uneven. In my sample list 3 letters took over 30% of the entire list. My aim was to present nested lists that could all be viewed without scrolling (between 10 and 12 items), but because of the above this is just not possible all the time. I only have the blocks here that create the first list for the listpicker, the A-D,E-H,etc. as the blocks probably need some optimisation before writing the whole thing out. But here goes:

Blocks:

blocksfirstnestedlist1

Lots of variables including an alphabet list

blocksfirstnestedlist2

The init and gotText fetching the external list of names (first and surname in each cell), calling three procedures and displaying various outputs

blocksfirstnestedlist3

In this procedure i get the first letters for each name, and then count the number of letters, and get those counts into a list, that corresponds with the alphabet list

blocksfirstnestedlist4

A simple one (courtesy of Ghica and others), that finds the letter with the biggest number of appearances. This is used to set the “optimal” size of the nested list.

blocksfirstnestedlist6blocksfirstnestedlist7

The big nasty one! In simple terms, this looks at the list of letter values and starts to build a list of sections. If the letter values equals the optimum value then it creates its own section (e.g. A-A), but if the letter values are less than this, i used a counter to add this to the next letter value until I had more than the optimum value, the created a section with the start and end letter. Finally, to complete the alphabet, check the last letter of the sections equals “Z”, if not create that section.
Screenshots:

appfirstnestedlist1

Red section is a list of all the items in the list by first letter
Blue section is a list of the count of each letter in the above
Pink section is the final list of section for List1 (F-L is the odd one as it has over 20 items in it)
Guess you can play around with the variables for the optimum size to get what you want

appfirstnestedlist2

Leave a Reply

Your email address will not be published. Required fields are marked *