Android Question Update list item

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
I have 2 Map data in list example :
B4X:
Dim l1 as list
l1.initialize

dim m1,m2 as map
m1.initialize
m1.put("name","Dennis")
m1.put("ID",1)

m2.initialize
m2.put("name","Erel")
m2.put("ID",2)

l1.add(m1)
l2.add(m2)

now assume i want to modify m2 variable data example i change "Erel" to "B4x"
how i can modify it in list?(my list size is large)
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim index As Int = 0
    Dim again As Boolean = True
    Do While(again)
        Dim m As Map = l1.Get(index)
        Dim s As String = m.GetDefault("name", "NotErel")
        If s=="Erel" Then 
            m.Put("name", "B4X")    'This overwrites the previous key, "Erel", to "B4X"
            Log("Name Changed")
            again = False
        End If
        index = index + 1
        If index==l1.Size Then again = False
    Loop
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim index As Int = 0
    Dim again As Boolean = True
    Do While(again)
        Dim m As Map = l1.Get(index)
        Dim s As String = m.GetDefault("name", "NotErel")
        If s=="Erel" Then
            m.Put("name", "B4X")    'This overwrites the previous key, "Erel", to "B4X"
            Log("Name Changed")
            again = False
        End If
        index = index + 1
        If index==l1.Size Then again = False
    Loop
Thank you but i do this way but not work
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I just ran this code in B4J:
B4X:
Dim l1 As List
l1.Initialize
Dim m1, m2 As Map
m1.Initialize
m2.Initialize
m1.Put("name", "Erel")
m1.Put("id", "three")
m2.Put("name", "Dennis")
m2.Put("id", "four")
l1.Add(m1)
l1.Add(m2)
Log("m1.name: " & m1.Get("name"))
Dim index As Int = 0
Dim again As Boolean = True
Do While(again)
    Dim m As Map = l1.Get(index)
    Dim s As String = m.GetDefault("name", "NotErel")
    If s=="Erel" Then 
        m.Put("name", "B4X")    'This overwrites the previous key, "Erel", to "B4X"
        Log("Name Changed")
        again = False
    End If
    index = index + 1
    If index==l1.Size Then again = False
Loop
Log("m1.name: " & m1.Get("name"))
and it produced this output in the log:
B4X:
Program started.
m1.name: Erel
Name Changed
m1.name: B4X
exactly as expected.

Are you doing something different? Some possible tripping points: make sure the map "m" is Dimmed INSIDE the loop so that it is created anew each iteration. Make sure your capitalization is consistent across Strings. For example, if you write "Erel" the first time, don't use "erel" later on. Make sure you are incrementing the index variable (index = index + 1) each time through the loop or else the loop will run until the end of the universe. Also, note that this code will only find the first map that has "Erel" as a value for the "name" key. If you have multiple maps with a "name":"Erel" entry, only the first one will be changed to "name":"B4X". However you can change that by not setting "again=false" after finding the first appearance of "Erel".
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
I just ran this code in B4J:
B4X:
Dim l1 As List
l1.Initialize
Dim m1, m2 As Map
m1.Initialize
m2.Initialize
m1.Put("name", "Erel")
m1.Put("id", "three")
m2.Put("name", "Dennis")
m2.Put("id", "four")
l1.Add(m1)
l1.Add(m2)
Log("m1.name: " & m1.Get("name"))
Dim index As Int = 0
Dim again As Boolean = True
Do While(again)
    Dim m As Map = l1.Get(index)
    Dim s As String = m.GetDefault("name", "NotErel")
    If s=="Erel" Then
        m.Put("name", "B4X")    'This overwrites the previous key, "Erel", to "B4X"
        Log("Name Changed")
        again = False
    End If
    index = index + 1
    If index==l1.Size Then again = False
Loop
Log("m1.name: " & m1.Get("name"))
and it produced this output in the log:
B4X:
Program started.
m1.name: Erel
Name Changed
m1.name: B4X
exactly as expected.

Are you doing something different? Some possible tripping points: make sure the map "m" is Dimmed INSIDE the loop so that it is created anew each iteration. Make sure your capitalization is consistent across Strings. For example, if you write "Erel" the first time, don't use "erel" later on. Make sure you are incrementing the index variable (index = index + 1) each time through the loop or else the loop will run until the end of the universe. Also, note that this code will only find the first map that has "Erel" as a value for the "name" key. If you have multiple maps with a "name":"Erel" entry, only the first one will be changed to "name":"B4X". However you can change that by not setting "again=false" after finding the first appearance of "Erel".
place map data in list and retest it,i think that not save in list
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
I can solve example below code : (change item of list and update item in list)
B4X:
    Dim l1 As List
    l1.Initialize
  
    Dim m1 As Map
    m1.Initialize
  
    m1.Put("Name","Omid")
    m1.Put("ID","1")
    l1.Add(m1)
  
    m1.Initialize
    m1.Put("Name","Erel")
    m1.Put("ID","2")
    l1.Add(m1)
  
    Dim m2 As Map
    m2.Initialize
    m2 = l1.Get(1)
    m2.Put("Name","Deniss")
    Log(l1)
 
Upvote 0
Top