iOS Question Get Country Names into a List

aaronk

Well-Known Member
Licensed User
Hi,

I am trying to load all the country's into a list.

In B4A I used the following code but of cause it don't work with B4i.

Is there a way in doing the following in b4i ?

B4X:
Sub GetCountryNames
    Dim jo As JavaObject
    Dim ISOCodes() As String

    jo.InitializeStatic("java.util.Locale")
    ISOCodes = jo.RunMethod("getISOCountries", Null)
    ISOCountryCodes.AddAll(ISOCodes)

    Country.Clear
    For i = 0 To ISOCountryCodes.Size - 1
        Dim jo1 As JavaObject
        jo1.InitializeNewInstance("java.util.Locale", Array As Object("en", ISOCountryCodes.Get(i)))
        Dim str0, str(2) As String
        str0 = jo1.RunMethod("getDisplayName", Null)
        str = Regex.Split("[(]", str0)
        Country.Add(str(1).Replace(")", ""))
    Next
End Sub
 

Erel

Administrator
Staff member
Licensed User
B4X:
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(countrycode & ": " & name.AsString)
   Else
       Log(countrycode)
   End If
Next
 
Upvote 0

aaronk

Well-Known Member
Licensed User
That seem to work but it seems to add the item multiple times.

I guess I can load it into a list and then remove any duplicated items.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
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):
B4X:
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:
B4X:
Public Country As List
    Dim AllCountry As Map

After the layout is loaded, I am running the following code:
B4X:
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:
B4X:
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 ?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
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.
This is a mistake. You should add the items to the map and then to the list.

For any other question please start a new thread.
 
Upvote 0
Top