Android Question Error: Map in a loop (XOM + DBUtils)

asales

Well-Known Member
Licensed User
Longtime User
I have this code to import a xml into database:

B4X:
Sub XOMBuilder1_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
    XOMDocument1.IsInitialized
    Dim RootElement As XOMElement
    RootElement = XOMDocument1.RootElement
    ItemsElements = RootElement.GetChildElementsByName("item")
     
    Dim i, ElementsCount As Int
    ElementsCount = ItemsElements.Size

    For i = 0 To ElementsCount - 1
        dbmap.Put("name", ItemsElements.GetElement(i).GetFirstChildElementByName("name").Value)
        dbmap.Put("address", ItemsElements.GetElement(i).GetFirstChildElementByName("address").Value)
        ListOfMaps.Add(dbmap)
    Next

      If ListOfMaps.Size > 0 Then
        DBUtils.InsertMaps(SQL1, "mytable", ListOfMaps)
       ListOfMaps.Clear
    End If
End Sub

but I received the message:
"Same Map found twice in list. Each item in the list should include a different map object."

This message is in DBUtils and explains:
"Small check for a common error where the same map is used in a loop"

How can I fix this code?
Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
try it like this

B4X:
    For i = 0 To ElementsCount - 1
        dim dbmap as Map
        dbmap.Put("name", ItemsElements.GetElement(i).GetFirstChildElementByName("name").Value)
        dbmap.Put("address", ItemsElements.GetElement(i).GetFirstChildElementByName("address").Value)
        ListOfMaps.Add(dbmap)
    Next
 
Upvote 0

asales

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred.

I need add only this line in your code:
B4X:
dim dbmap as Map
-> dbmap.Initialize
(...)
 
Upvote 0
Top