Android Question list does not add members

kalarius

Active Member
Licensed User
Longtime User
I write the follow code

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Type cuContacts (DisplayName As String,Id As Long)
Type ep (DisplayName As String,Id As Long)
Dim Epafh As ep

Private Button1 As Button
Private List1 As ListView
Dim cU As Contacts2
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("epafes")

'Epafh.Initialize
Dim cuContacts As List
cuContacts.Initialize

For Each c As Contact In cU.GetAll(True,False)
Epafh.DisplayName=c.DisplayName
Epafh.Id=c.Id
cuContacts.add(Epafh)

Next
cuContacts.SortType("DisplayName",True)

the problem is that when I trace the progrmam I can read the valuse of the epafes.id and epafh.displayname

When the for loop ends I stop the program and check what the cucontacts has inside the array All the lines (69) have the same displayname=admin@pandaray and id=6 (this is the last at the pnone book)

what is it wrong at my code?
 

kalarius

Active Member
Licensed User
Longtime User
i find it

inside the For loop I dim again the Epafh So the program uses a new instace

so the code is

For Each c As Contact In cU.GetAll(True,False)
dim epafh as ep ' ********************* this is the new line , solve the problem
Epafh.DisplayName=c.DisplayName
Epafh.Id=c.Id
cuContacts.add(Epafh)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Additionally:

A List is some sort of array which POINTS to objects you've added. Dim'ming once creates just ONE instance of an object. Adding the same object a lot of times adds a lot of pointers to the same object. If you then change the object (new value(s)) and browse the list all show the same value (because all entries point to the same object). I had this at the beginning, too :)
 
Upvote 0
Top