Android Question CustomControl with CustomListView Item Click Event Problem

Guenter Becker

Active Member
Licensed User
Hi,
I am just developing a custom control. In the control class I add a customlistview and its list items by code. I want to capture the event if the user clicks on a list row.
In the event sub I want to transfer the row value/index of the clicked list row to the main activity which loads the customcontrol as part of a layout.

I tried some solutions but nothing works. Please find attached my current code snipped. Can anyone help me how to answer the two questions of writing the code for the click event of the customlistview and the code for the index/value transfer to the parent activity. Thank you in advance.

Mainpart of customcontrol class:
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView
    Private xui As XUI 'ignore
    Public Tag As Object
    
    Type TMI ( _
        ID As Int, _
        ParentID As Int, _
        BM As Bitmap, _
        Txt As String, _
        Show As Boolean)
    Private clv As CustomListView
    Public ItemList As List
    Private props1 As Map
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
    
    'Scv.Initialize(100dip)
    clv.Initialize(clv,"clv") <<<<<<<<<<< Is This is correct?
    ItemList.Initialize
    props1.Initialize
End Sub

'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, props As Map)
    mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me
        
    props1 = props
    ' set parameters
    '.Width=mBase.Width
    'Scv.height= mBase.height
    mBase.AddView(clv.AsView,0,0,mBase.Width,mBase.Height) <<<<<<<<<<< Is This is correct?
    
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
 
End Sub

Sub buildBranch(item As TMI) As Panel
    
    Dim t As Label ' Branch label
    t.Initialize("ItemClicked")
    t.Text = item.txt
    If item.ParentID = 0 Then
        t.Textcolor = props1.Get("ParentTextColor")
        t.TextSize = props1.Get("ParentTextSize")
    Else
        t.Textcolor = props1.Get("ChildTextColor")
        t.TextSize = props1.Get("ChildTextSize")
    End If
    t.Height = t.TextSize + 10
    If item.ParentID <> 0 Then t.Tag = item.parentid
    
    Dim ind As Label ' Intend
    ind.Initialize("ItemClicked")
    ind.Width = props1.Get("IntendWidth")
    ind.Height = t.height
    If item.ParentID <> 0 Then ind.tag = item.parentid
    
    Dim i As ImageView ' Branch Image
    i.Initialize("ItemClicked")
    i.Height = t.Height
    i.Width = i.Height
    If item.ParentID <> 0 Then i.Tag=item.parentid
    If item.bm.isinitialized Then i.Bitmap=item.bm
    
    ' stretch label to full width
    t.Width =clv.asview.Width -10 - i.Width -10 -10

    Dim p As Panel ' Main Branchpanel
    p.Initialize("p")
    p.RemoveAllViews
    p.Height = t.height
    If item.ParentID <> 0 Then p.Tag = item.parentid
    If p.Height < i.Height Then p.Height=i.height
    If p.Height < t.Height Then p.Height=t.height
    If item.ParentID=0 Then
        p.Color = props1.Get("ParentColor")
    Else
        p.Color = props1.Get("ChildColor")
    End If   
    ' add views to the panel
    If item.ParentID = 0 Then
        p.addview(i,10,0,i.Width,i.Height)
        p.AddView(t,10 + i.Width + 10,0,t.Width,t.height)
        p.Width = 10 + i.Width + 10 + t.width
    Else
        p.addview(ind,10,0,ind.width,ind.height)   
        p.addview(i,10 + ind.Width +10,0,i.Width,i.Height)
        p.AddView(t,10 + ind.width + 10 + i.Width +10,0,t.Width,t.height)
        p.Width = 10 + ind.Width + 10 + i.Width+10+t.width
    End If
    ' return panel as tree row
    Return p
End Sub

public Sub buildTree
    Dim item As TMI
    Dim p As Panel
    p.Initialize("p")
    clv.clear
    For x = 0 To ItemList.Size-1
        item = ItemList.Get(x)
        'Log("V" & r & " " & item.Txt & " " & Scv.height)
        p = buildBranch(item)
        'Scv.Panel.AddView(p,0,r,p.Width,p.Height)
        If item.Show = True Then clv.Add(p,p.Height,x)
    Next

End Sub


Sub clv_ItemClick (Index As Int, Value As Object) <<<<<<<<<<<<<<< This is the not working event sub
    
    CallSub(?????????) <<<<<< This should be the Transfer to the paren activity
End Sub

Some codelines in B4A would help me.
Stay well and best regards
Guenter
 

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
In short, you want to transfer the value of the index you get from clicking from one activity to another.
1. Check out Callsub, CallSub2 etc.
B4X:
Sub clv_ItemClick (Index As Int, Value As Object) <<<<<<<<<<<<<<< This is the not working event sub
  CallSub2(Main,"YOU>>>subActivitiMain",Index) <<<<<< This should be the Transfer to the paren activity
End Sub

2.Write the index value to a text file and then read it in a new activity
B4X:
File.WriteString(File.DirInternal,"1.txt",indeks)
'Activiti Main
 Dim a as int
 a=File.ReadString(File.DirInternal,"1.txt")
3.The latest approach is:
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
an adaption of code for my custom view ..

Class module...
B4X:
Private Sub  clv_ItemClick(Index As Int, Value As Object)	
	If SubExists(mCallBack, mEventName & "_ItemClick") Then
		'raise the Click event , passing selected parameters
		CallSub3(mCallBack, mEventName & "_ItemClick", Index, Value)
	End If

and in the Main Activity ...
B4X:
Private CLV1 As MyCustomControl

Sub CLV1_ItemClick(Index As Int, Value As Object)
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
an adaption of code for my custom view ..

Class module...
B4X:
Private Sub  clv_ItemClick(Index As Int, Value As Object) 
    If SubExists(mCallBack, mEventName & "_ItemClick") Then
        'raise the Click event , passing selected parameters
        CallSub3(mCallBack, mEventName & "_ItemClick", Index, Value)
    End If

and in the Main Activity ...
B4X:
Private CLV1 As MyCustomControl

Sub CLV1_ItemClick(Index As Int, Value As Object)

Thank you for the answer sounds good will try it. But there is still an unsolved problem that the event sub clv_Itemclick is not fired? Is my intialization of clv.initialize(clv,"clv") wrong? I tried also clv.initialize(mcallback,"clv"). As I run the code I can see that the listitem is clicked but there is no jump to the sub.
It seems to me that the initialization may wrong?

cheers Guenter
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
Hi
In short, you want to transfer the value of the index you get from clicking from one activity to another.
1. Check out Callsub, CallSub2 etc.
B4X:
Sub clv_ItemClick (Index As Int, Value As Object) <<<<<<<<<<<<<<< This is the not working event sub
  CallSub2(Main,"YOU>>>subActivitiMain",Index) <<<<<< This should be the Transfer to the paren activity
End Sub

2.Write the index value to a text file and then read it in a new activity
B4X:
File.WriteString(File.DirInternal,"1.txt",indeks)
'Activiti Main
Dim a as int
a=File.ReadString(File.DirInternal,"1.txt")
3.The latest approach is:

Thank you for the answer sounds good will try it. But there is still an unsolved problem that the event sub clv_Itemclick is not fired? Is my intialization of clv.initialize(clv,"clv") wrong? I tried also clv.initialize(mcallback,"clv"). As I run the code I can see that the listitem is clicked but there is no jump to the sub.
It seems to me that the initialization may wrong?

cheers Guenter
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
Ok thank you!
Have to stop working on that problem due to higher prio of other tasks.
Will welcome your proposals and try them later.
Thank you for assissting
see you and stay well
cheers Guenter
 
Upvote 0
Top