List in Type Declaration : lost data ?

fabioferreiracs

Member
Licensed User
Longtime User
I declared a Type in the Process Global and created a variable of arrays with this type.
The Type contains a list of values​​.
the Array variable ( ts ) is declared in "Global process" and initialized in the main activity.

the Activity_create starts a service (StartService (serv)) and this service gives values ​​of the variables Type.

However, the intention is that each element of the array contained a single list.
B4A not see how unique each list for each array?

In debug , ".ListaEntidades" is shared for all elements of the array ts().

I need this :

B4X:
main.ts(0).listaEntidades : (ArrayList) [0.0, 1.0]
main.ts(1).listaEntidades : (ArrayList) [11.0, 12.0]
main.ts(2).listaEntidades : (ArrayList) [22.0, 23.0]

and i have this :

B4X:
main.ts(0).listaEntidades :  (ArrayList) [22.0, 23.0]
main.ts(1).listaEntidades :  (ArrayList) [22.0, 23.0]
main.ts(2).listaEntidades :  (ArrayList) [22.0, 23.0]

DEBUG:
B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Service (serv) Create **


** Service (serv) Start **


 
--comecar inicio --
myList : (ArrayList) [0.0, 1.0]
main.ts(0).seq :  0


main.ts(0).name :  name0
main.ts(0).seq :  (ArrayList) [0.0, 1.0]
main.ts(0).listaEntidades.size :  2
myList : (ArrayList) [11.0, 12.0]


main.ts(1).seq :  10
main.ts(1).name :  name10
main.ts(1).seq :  (ArrayList) [11.0, 12.0]


main.ts(1).listaEntidades.size :  2
myList : (ArrayList) [22.0, 23.0]
main.ts(2).seq :  20


main.ts(2).name :  name20
main.ts(2).seq :  (ArrayList) [22.0, 23.0]
main.ts(2).listaEntidades.size :  2
--comecar fim --


 


Conferer
ts(0).seq :  0
ts(0).name :  name0
ts(0).seq :  (ArrayList) [22.0, 23.0]
ts(0).listaEntidades.size :  2
 
Conferer
ts(1).seq :  10
ts(1).name :  name10
ts(1).seq :  (ArrayList) [22.0, 23.0]
ts(1).listaEntidades.size :  2
 
Conferer
ts(2).seq :  20
ts(2).name :  name20
ts(2).seq :  (ArrayList) [22.0, 23.0]
ts(2).listaEntidades.size :  2


The Main Activity Code :
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Type twostrings ( seq As Int, name As String , listaEntidades As List)
   Dim ts(3) As twostrings
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   
   
   For i  = 0 To ts.Length - 1
      ts(i).Initialize
   Next
   
   StartService(serv)

   ' CallSub(serv , "comecar")
   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub conf
For x =  0 To ts.Length - 1
   Log(" ")
   Log("Conferer")
   Log("ts(" & x & ").seq :  " & ts(x).seq)
   Log("ts(" & x & ").name :  " & ts(x).name)
   Log("ts(" & x & ").seq :  " & ts(x).listaEntidades)
   Log("ts(" & x & ").listaEntidades.size :  " & ts(x).listaEntidades.size)
   
Next

End Sub



The "serv" code service module :

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.


End Sub
Sub Service_Create



End Sub

Sub Service_Start (StartingIntent As Intent)

comecar

End Sub

Sub Service_Destroy

End Sub
Sub comecar 
Dim myList As List
myList.initialize
Log(" ")
Log("--comecar inicio --")
Dim z = 0
For x =  0 To Main.ts.Length - 1
   myList.Clear
   myList.add (x + z )
   myList.add (x + 1 + z )

   
   Log("myList : " & myList)


   Main.ts(x).seq = z
   Main.ts(x).name = "name" & z
   Main.ts(x).listaEntidades = myList
   
   
   Log("main.ts(" & x & ").seq :  " & Main.ts(x).seq)
   Log("main.ts(" & x & ").name :  " & Main.ts(x).name)
   Log("main.ts(" & x & ").seq :  " & Main.ts(x).listaEntidades)
   Log("main.ts(" & x & ").listaEntidades.size :  " & Main.ts(x).listaEntidades.size)
   z = z + 10
Next

Log("--comecar fim --")

CallSub(Main, "conf")

End Sub


how i do ?
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
you can move
B4X:
Dim myList As List
myList.initialize
at the beginning of your 'for x' loop.
 
Upvote 0
Top