Need help with using MAP to hold data

netchicken

Active Member
Licensed User
Longtime User
I am investigating using MAP to store my data, if it runs like a hashtable it will be great. However I have found only a few lines of code and not one actual layout on how to use it.

Can someone please help?

Here is a database call that happily loads the listview, I want the data to also pass to the map so I can call it later. The DB call loads the listivew happily, just doesn't load the MAP

B4X:
 Sub DBload ' load the alternative names db
   
'need to clear the list
lvdb.Clear

cursor1 = SQL1.ExecQuery("SELECT * FROM name")
    For i = 0 To cursor1.RowCount - 1
    cursor1.Position = i
    
    lvdb.AddSingleLine(cursor1.GetString("ID")& " : " &cursor1.GetString("name")& " = " &cursor1.GetString("altname")) 
    
'this works and loads up the data into the listview 
lvdb.SingleLineLayout.Label.TextSize = 15
lvdb.SingleLineLayout.ItemHeight = 40
lvdb.SingleLineLayout.label.TextColor = Colors.Black
lvdb.ScrollingBackgroundColor =Colors.Transparent
      
'map here not working, it returns just the Key: mapname , Value: mapaltname
'I suspect its just overwriting the same data field yet its is looping with the main loop the cursor1.getString("name") should work as it does above.

Dim mapname, mapaltname As String
mapname=cursor1.getString("name")
mapaltname=cursor1.getString("altname")
   allmapnames.Put("mapname", "mapaltname")   
Log("mapname " & mapname)
Log("mapaltname " & mapaltname)   
      Next
    
End Sub



At the other end I want to get out the value when the key matches. i tried a number of ways but of course as it seems the data isn't going in, it certainly won't look good coming out. using a hash table I don't remember having to iterate across the keys.

B4X:
  If From = PhoneNum Then
Log(Contact.DisplayName)     
'add custom names to here with MAP
For i = 0 To allmapnames.Size - 1
If allmapnames.ContainsKey(Contact.DisplayName) Then
'If Contact.DisplayName = allmapnames.ContainsKey(i) Then
Contact.DisplayName = allmapnames.GetValueAt(Contact.DisplayName)

End If
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
What you are doing in this line:
B4X:
allmapnames.Put("mapname", "mapaltname")
Is writing and overwriting the "mapname" key with "mapaltname", it should be:
B4X:
allmapnames.Put("mapname", mapname)
allmapnames.Put("mapaltname", mapaltname)

You also have that in a loop, so, you are overwriting the values (more info about MAP HERE)

You need to use unique key names, using maps in this case is the wrong choice.
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
I solved the input side, thanks NJdude for the ideas. i had " " around my fields by mistake.

This puts the name and the the nickname into a map,The key field is unique so its OK.

B4X:
 mapname=cursor1.getString("name")
mapaltname=cursor1.getString("altname")
allmapnames.Put(mapname, mapaltname)


At the other end I have 90% of it done this passes out the value.
B4X:
For i=0 To allmapnames.size-1 
If allmapnames.ContainsKey(Contact.DisplayName) = True Then
   altname = allmapnames.GetValueAt(i)
End If
 
Upvote 0
Top