Android Question Problem with CustomListView with Structure as Value

renepatriana

Member
Licensed User
Hi.

I am using a CustomListView (xCustomListView v1.64 from Erel) and when adding items the value does not behave well when it is a structure (Type).

CustomListView1.Add(CreateItem(Line1, Line2, Line3), Structure1)

It works perfect when the value is a number or a string, but when it is a structure, the value stored in each item corresponds to the last one stored in the structure, regardless of whether the data has been correctly assigned to the structure for each item.

Any ideas?

Thanks for your time.
 

Erel

Administrator
Staff member
Licensed User
This is a common mistake. It is discussed in this tutorial:

 

renepatriana

Member
Licensed User
I'm doing something like this:

Type KeyStructure(Data1 As String, Data2 As Int, Data3 As String)

Dim Structure1 As KeyStructure

For Counter = 0 To List1.Size - 1

Map1 = List1.Get(Counter)

Line1 = Map1.Get("field1")
Line2 = Map1.Get("field2")
Line3 = Map1.Get("field3")

Structure1.Initialize

Structure1.Data1 = Map1.Get("code1")
Structure1.Data2 = Map1.Get("code2")
Structure1.Data3 = Map1.Get("code3")

CustomListView1.Add(CreateItem(Line1, Line2, Line3), Structure1)

Next

When I doing click on the CustomListView1

Dim LocalStructure As KeyStructure
LocalStructure = CustomListView1.GetValue(Index)

LocalStructure always has the same value that corresponds to the last data given in the reading above.
 

DonManfred

Expert
Licensed User
1. I already told you that you should not reuse the same object.
2. Erel posted a video which describes exactly this problem.

You just ignore both hints.

B4X:
For Counter = 0 To List1.Size - 1

Map1 = List1.Get(Counter)

Line1 = Map1.Get("field1")
Line2 = Map1.Get("field2")
Line3 = Map1.Get("field3")
Dim Structure1 As KeyStructure
Structure1.Initialize

Structure1.Data1 = Map1.Get("code1")
Structure1.Data2 = Map1.Get("code2")
Structure1.Data3 = Map1.Get("code3")

CustomListView1.Add(CreateItem(Line1, Line2, Line3), Structure1)

Next
 

renepatriana

Member
Licensed User
All good. Beginner error. The "dim" of the structure is established within the "for" and the problem is corrected. Thank you.
 
Top