List doesn't seem to be updating

andymc

Well-Known Member
Licensed User
Longtime User
Why doesn't this code work???? I want to write to a list of objects, then cycle through the list to populate a listview, but the list view just shows the last entry from the list all the time!!!

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Dates")
   
   'initialize dates list
   datesList.Initialize
   'initialize date object
   lvd.Initialize
   
   'first date object
   lvd.Name = "First Date"
   lvd.Day = 24
   lvd.Month = 4
   lvd.Year = 2009
   'add date object to dates list
   datesList.Add(lvd)
   
   'second date object
   lvd.Name = "Got Engaged"
   lvd.Day = 30
   lvd.Month = 12
   lvd.Year = 2011
   'add object to list
   datesList.Add(lvd)
   
   'loop through list and write entries as strings to listView
   For x = 0 To datesList.Size-1
      lvd = datesList.Get(x)
      lv_dates.AddTwoLines(lvd.Name & " - " & x,lvd.Day & " - " & getMonth(lvd.Month) & " - " & lvd.Year)
   'returning same value each time!!!!
   Next
End Sub
 

Ricky D

Well-Known Member
Licensed User
Longtime User
You need to dim lvd again before the 2nd one and also before any others you add. Don't ask me why. Doing this fixed my problem.

Regards, Ricky
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Yes, you have to add a Dim when you want to reuse the object for another record.
This is because your variable lvd is a custom type, the data is stored in one location, but lvd points to the address of this location.
When you re-assign lvd, it changes the data in the first location.
If you Dim again, it will create a new object.
 
Upvote 0
Top