Add the items to a Map as the keys.
Yeah, that is what I was planning to do.
I think I might be doing something wrong as it's lagging and very slow.
I am using a B4XComboBox as well. I am loading the country's into the combobox, but after the page loads and I tap on the ComboBox it crashes the app.
First I am loading the Country's into the List (just so I can sort the list). I am then moving the items from the list to a map so I can remove any duplicated items. I am then loading the items from the map into the ComboBox.
I have added the following to the B4XComboBox Class (everything else remained the same in the class):
Public Sub AddItem(Item As String)
#if B4J
cmbBox.Items.Clear
cmbBox.Items.AddAll(Items)
#Else If B4A
cmbBox.Clear
cmbBox.AddAll(Items)
#Else If B4i
If mItems.IsInitialized = False Then
Dim mItems As List
mItems.Initialize
End If
mItems.Add(Item)
mSelectedIndex = -1
#End If
End Sub
In a code module (the page I am showing the ComboBox) I have the following code in the Progress_Global sub:
Public Country As List
Dim AllCountry As Map
After the layout is loaded, I am running the following code:
Country.Initialize
GetCountryNames
For i = 0 To AllCountry.Size - 1
For Each key As String In AllCountry.Keys
B4XComboBox2.AddItem(AllCountry.Get(key))
Next
Next
I am sorting the country's like the following:
Sub GetCountryNames
Dim no As NativeObject
Dim countries As List = no.Initialize("NSLocale").GetField("availableLocaleIdentifiers")
no = no.Initialize("NSLocale").GetField("currentLocale")
For Each countrycode As String In countries
Dim name As NativeObject = no.RunMethod("localizedStringForCountryCode:", Array(countrycode))
If name.IsInitialized Then
'Log(name.AsString)
Country.Add(name.AsString)
Else
'Log(countrycode)
End If
Next
Country.Sort(True) ' sort them from A-Z
AllCountry.Initialize
For i = 0 To Country.Size - 1
AllCountry.Put(Country.Get(i),Country.Get(i))
Next
End Sub
Is this the best way in doing it ?