Android Question Update list item

Discussion in 'Android Questions' started by omidaghakhani1368, Jul 11, 2015.

Similar threads

B4A Tutorial Google Maps
B4A Code Snippet Collision Physics
B4A Question map question
B4A Question Basic JSON
B4A Question How is this possible?
  1. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Hi.
    I have 2 Map data in list example :
    Code:
    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)
     
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    Code:
    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
     
  3. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Thank you but i do this way but not work
     
  4. Roycefer

    Roycefer Well-Known Member Licensed User

    I just ran this code in B4J:
    Code:
    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:
    Code:
    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".
     
  5. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    place map data in list and retest it,i think that not save in list
     
  6. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    I can solve example below code : (change item of list and update item in list)
    Code:
    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)
     
Loading...