Android Question custom object click in customlistview (parent click)

Kiran Raotole

Active Member
Licensed User
I attach sample project.
I add my customobject in customlistview.
My problem is, When I click on my customobject, it doesn't run customlistview click,
on other side B4A default object work properly.
 

Attachments

  • temp1.zip
    16.3 KB · Views: 237

Kiran Raotole

Active Member
Licensed User
That's expected. Why aren't you handling the blabel_click event:
B4X:
Sub blabel1_Click
  
End Sub
You can find the clicked clv item with:
B4X:
Dim index As Int = CustomListView1.GetItemFromView(Sender)
I'm using twenty custom object in that panel.
So it's not possible to add command in each custom object.

Is their any possibility that I do in my Custom Object Class?
 
Upvote 0

Yayou49

Active Member
Licensed User
No need to set custom objects one by one.
As describe by Erel, the Sub blabel1_Click will be raised each time you'll click on a label (whatever the label you click on).
So, normaly, using Dim index As Int = CustomListView1.GetItemFromView(Sender) in the same sub will give you the Id of custom object clicked.

@Erel : By the way, it seems in the attached project, CustomListView1.GetItemFromView(Sender) raised an error on sub
B4X:
Public Sub GetItemFromView(v As B4XView) As Int
Error occurred on line: 469 (CustomListView)
java.lang.ClassCastException: b4a.example.blabel cannot be cast to android.view.View
 
Upvote 0

Yayou49

Active Member
Licensed User
In your case yes !
if you click on blabel1 or blabel2 or blabel3, or ........
you will always go under blable1_click event
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub listview_show
    Dim a = "aaaaaa" As String
    For i = 1 To 10
        CustomListView1.Add(add_row("custom view #"&i,"default view #"&i),a)
    Next
End Sub

Sub add_row(a1 As String,a2 As String) As B4XView
    Dim xui As XUI
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,100%x,60dip)
    p.LoadLayout("main_listview")
    blabel1.Text = a1 ' set thte label text
    Label1.Text = a2  ' set the 2nd label text
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    'Msgbox("customview click event run","")
End Sub

'Sub Label1_Click
''    Msgbox("default label click",False)
'End Sub

Sub blabel1_Click
    Log($"blabel1_Click"$)
    Dim lbl As blabel = Sender
    Dim p As Label = lbl.GetBase.GetView(0)
    Log(p.Text) ' Note to see the Text from the blabel you define earlier...

End Sub
 
Upvote 1

Yayou49

Active Member
Licensed User
Nice shot !!!

You can also use the tag property if you don't want to see the # in label text ....

B4X:
Sub listview_show
    Dim a = "aaaaaa" As String
    For i = 1 To 10
        CustomListView1.Add(add_row("custom view","default view",i),a)
    Next
    
End Sub

Sub add_row(a1 As String,a2 As String,i As Int) As B4XView
    Dim xui As XUI
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,100%x,60dip)
    p.LoadLayout("main_listview")
    blabel1.Text = a1 ' set thte label text
    Label1.Text = a2  ' set the 2nd label text
    Label1.Tag = i
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    Msgbox("customview click event run","")
End Sub

Sub blabel1_Click
    
    Log($"blabel1_Click"$)
    Dim lbl As blabel = Sender
    Dim p As Label = lbl.GetBase.GetView(0)
    Log(p.tag) 
    
End Sub
 
Upvote 0
Top