Android Question Getting index of panel added to customlistview

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I have a customlistview which adds each item as a panel, and I need to capture click and longclick events on these, with various results depending on which panel is clicked.

A few lines to clarify my setup:

B4X:
clv_contacts.Add(CreateListItem_contactlist(ct,clv_contacts.AsView.Width,50dip), 50dip, ct)

B4X:
Sub CreateListItem_contactlist(ct As Contactentries, itemWidth As Int, itemheight As Int) As Panel
    Dim p As Panel
    p.Initialize("")
   
    Activity.AddView(p, 0, 0, itemWidth, itemheight)

    p.LoadLayout("contacts_cellItem")
   
    p.RemoveView
        contact_Label.Text =  ct.contact_Name
    contact_Label.Tag = ct.contact_Name

    contact_imgpanel.Width = contact_image.Width
    contact_imgpanel.height = contact_image.height
    Dim cdw As ColorDrawable

    Return p
End Sub

As you can see I added a tag on the panel, but I´ve realized that since in the click and longclick events there´s no index, of course the tag will always contain only the last item added.
Does anybody have an idea of how this can be elegantly solved?

Thanks in advance!
 

DonManfred

Expert
Licensed User
Longtime User
As you can see I added a tag on the panel
Aehm. Wrong. You are adding a Tag on a contact_label.

If you want to distinguish the panel itself you can - for ex - add a parameter

B4X:
Sub CreateListItem_contactlist(ct As Contactentries, itemWidth As Int, itemheight As Int, Tag as Object) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Tag = Tag  ' add the tag to the Panel to find the right panel later
    Activity.AddView(p, 0, 0, itemWidth, itemheight)
    p.LoadLayout("contacts_cellItem")
 
    p.RemoveView
        contact_Label.Text =  ct.contact_Name
    contact_Label.Tag = ct.contact_Name

    contact_imgpanel.Width = contact_image.Width
    contact_imgpanel.height = contact_image.height
    Dim cdw As ColorDrawable

    Return p
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Aehm. Wrong. You are adding a Tag on a contact_label.

If you want to distinguish the panel itself you can - for ex - add a parameter

B4X:
Sub CreateListItem_contactlist(ct As Contactentries, itemWidth As Int, itemheight As Int, Tag as Object) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Tag = Tag  ' add the tag to the Panel to find the right panel later
    Activity.AddView(p, 0, 0, itemWidth, itemheight)
    p.LoadLayout("contacts_cellItem")

    p.RemoveView
        contact_Label.Text =  ct.contact_Name
    contact_Label.Tag = ct.contact_Name

    contact_imgpanel.Width = contact_image.Width
    contact_imgpanel.height = contact_image.height
    Dim cdw As ColorDrawable

    Return p
End Sub

You´re right of course - it´s not the panel I added the tag to - thanks for making me see this :)
I couldn´t get the panel event to fire though, so I went back to work with the label and got it working doing the following in the contact_label_click event:

B4X:
Dim l As Label
    l = Sender
    Log ("here´s the tag " & l.Tag)

The log shows that it now knows which item (label) that was clicked.
 
Last edited:
Upvote 0
Top