Android Question Problem with CustomListView with Structure as Value

Discussion in 'Android Questions' started by renepatriana, Oct 18, 2018.

  1. renepatriana

    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.
     
  2. DonManfred

    DonManfred Expert Licensed User

    it does work for me perfect.
    So you are reusing the type object instead of creating a new one each time. Sounds like here is the problem.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    This is a common mistake. It is discussed in this tutorial:

     
  4. renepatriana

    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.
     
  5. DonManfred

    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.

    Code:
    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
     
    Erel and Jeffrey Cameron like this.
  6. renepatriana

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice