Android Question Put Array data to map

Mitesh_Shah

Member
Licensed User
Longtime User
Hi how to put array data in map ?
&
get map data to array
?

B4X:
'// we try to >>

dim a(20) as int
dim b(20) as int
dim c(20) as int

Dim item As Map

item.put(0, a(0), b(0), c(0))        'try to add array data to map
have any example for that ?
 

emexes

Expert
Licensed User
Longtime User
'// we try to >> dim a(20) as int dim b(20) as int dim c(20) as int Dim item As Map item.put(0, a(0), b(0), c(0)) 'try to add array data to map

There are four easy ways to bunde up multiple items to store in a Map element:

1/ Array (usually of Objects)
2/ List
3/ another Map
4/ Custom Type / User Defined Type

so maybe something like this is where you're heading (I'm just guessing at what data you might be storing) :

B4X:
Sub Process_Globals
    Type StoreLocationType(StoreNumber As Int, Latitude As Int, Longitude As Int)
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
 
    '// we try to >>

    Dim a(20) As Int
    Dim b(20) As Int
    Dim c(20) As Int

    'create sample random data
    For I = 0 To a.Length - 1
        a(I) = Rnd(1000, 9999 + 1)    'random store number
        b(I) = Rnd(-90, 90 + 1)    'latitude
        c(I) = Rnd(-180, 180)    'longitude
    Next
 
    Dim item As Map
    item.Initialize

    '''item.put(0, a(0), b(0), c(0))        'try to add array data to map

    'copy data from arrays into map (note that if the same store number appears more than once, then older store data will be replaced by newer store data)
    For I = 0 To a.Length - 1
        Dim StoreLocation As StoreLocationType
        StoreLocation.StoreNumber = a(I)
        StoreLocation.Latitude = b(I)
        StoreLocation.Longitude = c(I)

        item.Put(StoreLocation.StoreNumber, StoreLocation)    'if store number already exists in map, it will be replaced by this newer data
    Next

    'retrieve data from Map (in StoreNumber order, because we're For'ing through potential store numbers from 1000 to 9999)
    For I = 1000 To 9999
        If item.ContainsKey(I) Then
            Dim sl As StoreLocationType = item.Get(I)
            Log(sl.StoreNumber & TAB & "lat " & sl.Latitude & TAB & "lon " & sl.Longitude)
        End If
    Next

End Sub
Log output:
Waiting for debugger to connect...
Program started.
Hello world!!!
1094    lat -48    lon -89
1966    lat -39    lon 5
2597    lat -75    lon -131
2824    lat 7      lon -15
3094    lat 27     lon 97
3546    lat -12    lon -39
3745    lat 47     lon -21
3990    lat 74     lon 82
4381    lat -46    lon 36
4574    lat 35     lon 119
5272    lat 84     lon -173
5280    lat -42    lon -110
5499    lat -16    lon -5
5718    lat 0      lon 124
6657    lat -55    lon 179
6822    lat -49    lon 155
7545    lat -84    lon 113
7867    lat 75     lon -160
9199    lat -5     lon 2
9458    lat -72    lon -118
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
4/ Custom Type / User Defined Type

StoreLocationType is a Custom Type

 
Upvote 0

Mitesh_Shah

Member
Licensed User
Longtime User
get some error

to add StoreLocationTyp

i m use b4xpage

error.jpg
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
It should be .. StoreLocationType

B4X:
Dim StoreLocation As StoreLocationType
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
If you give us a better clue about what you're doing, maybe we can give you an answer better suited to it. 🍻

But now worries either way. 🙃
 
Upvote 0
Top