Android Question [SOLVED] - Selecting a custom list view item for a specific index number

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

In my custom list view I'm using this code to play and pause music. When the user clicks the play icon the icon changes to a pause icon so the user can pause the music. Clicking it again will make the icon a play icon.

I know what the index is for the play / pause ImageView. I want to select the correct item row in the custom list view for that index because clicking the ImageView doesn't raise _ItemClick.

B4X:
Sub ImageViewPlayPauseSong_Click
   
    Dim ViewThatRaisedThisEvent As ImageView = Sender

    If blnPlayCurrentSong Then
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "play-circle.png")
    Else
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "pause-circle.png")
    End If
   
    Log("Index: " & ViewThatRaisedThisEvent.Tag)

    ImageViewPlayCurrentSong_Click
End Sub
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
You can get the item index with CLV.GetItemFromView.
Hi Erel.

Thanks for the reply and the hint which I will use to eliminate needing to mess around with the tag property for the index, but I'm trying to select the list item in code. I did find JumpToItem and ScrollToItem but they don't actually select the item because I have a log statement in the ItemClick handler of the clv and that handler is not activated when using the jump and scroll. It only is activated when I actually tap the screen on the list and not on the ImaveView. Maybe there is something like SelectToItem(index) or SetFocusToListItem(index) or a way to cause the ClickItem handler to be activated?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
It only is activated when I actually tap the screen on the list and not on the ImaveView
If you want the index of the item that holds the clicked imageview, use the imageview click event:
B4X:
Sub ViewThatRaisedThisEvent_Click   'your imageview click event
    'To get the index of the panel of the image clicked
    Dim index As Int = CLV1.GetItemFromView(Sender)  'you customlistview is : CLV1
    Log("panel index of image clicked " &  index)
    If blnPlayCurrentSong Then
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "play-circle.png")
    Else
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "pause-circle.png")
    End If
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
If you want the index of the item that holds the clicked imageview, use the imageview click event:
B4X:
Sub ViewThatRaisedThisEvent_Click   'your imageview click event
    'To get the index of the panel of the image clicked
    Dim index As Int = CLV1.GetItemFromView(Sender)  'you customlistview is : CLV1
    Log("panel index of image clicked " &  index)
    If blnPlayCurrentSong Then
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "play-circle.png")
    Else
        ViewThatRaisedThisEvent.Bitmap  = LoadBitmap(File.DirAssets, "pause-circle.png")
    End If
End Sub
Hi Mahares,

Thanks for the useful code. I'm also looking to select the item in the CLV for that index so the ItemClick event is triggered. Do you know how to do that?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
select the item in the CLV for that index so the ItemClick event is triggered
You cannot click on the image if you have code for imageclick event, unless you have available space away from the imgv to click the item.
In the below example, I change the size of the image for the 1st imageview:
B4X:
Sub CLV1_ItemClick (Index As Int, Value() As Object)
    Log(Index)
    Dim panel As B4XView = CLV1.GetPanel(Index)
    Dim iv As ImageView =panel.GetView(0).GetView(0)  'assuming you have a base panel and the imageview is the 1st view in that panel. This can vary
    iv.bitmap = LoadBitmapResize(File.DirAssets, "Hannibal.jpg", panel.Width / 4, panel.Height/2, True)
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
You cannot click on the image if you have code for imageclick event, unless you have available space away from the imgv to click the item.
In the below example, I change the size of the image for the 1st imageview:
B4X:
Sub CLV1_ItemClick (Index As Int, Value() As Object)
    Log(Index)
    Dim panel As B4XView = CLV1.GetPanel(Index)
    Dim iv As ImageView =panel.GetView(0).GetView(0)  'assuming you have a base panel and the imageview is the 1st view in that panel. This can vary
    iv.bitmap = LoadBitmapResize(File.DirAssets, "Hannibal.jpg", panel.Width / 4, panel.Height/2, True)
End Sub
Hi Mahares,

Nice code. Do you know how to select an item from the CLV using coding?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Not sure what you mean here. Can you post the code for what you have in mind.
Hi Mahares,

Sure. The code is in post number 1.

I already looked at the auto-complete dropdown when typing the name of my CLV plus the full stop. I found JumpToItem and ScrollToItem. Those don't work for me because they only reposition the CLV item so it can be seen. They don't actually select the item from the CLV. I need to select the item in code so the ItemClick handler will be triggered.

I plan on putting the code to select the CLV item after the IF statement.

Thanks.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I plan on putting the code to select the CLV item after the IF statement.
To select an item from the CLV, you have to use this sub that I inserted in post 6 to get its index. You have to invoke the CLV1_ItemClick event. Remember the item is a panel that holds one or many views, like labels, buttons, imageviews, etc.:
Sub CLV1_ItemClick (Index As Int, Value() As Object)
If what you have in mind is totally different from what I perceive, then I will leave this to a powerhouse like Erel. to help with
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
To select an item from the CLV, you have to use this sub that I inserted in post 6 to get its index. You have to invoke the CLV1_ItemClick event. Remember the item is a panel that holds one or many views, like labels, buttons, imageviews, etc.:
Sub CLV1_ItemClick (Index As Int, Value() As Object)
If what you have in mind is totally different from what I perceive, then I will leave this to a powerhouse like Erel. to help with
Since I now know what the index is I will skip the selecting of the CLV item. I just wanted to simulate tapping that item first but the ultimate part is getting the index value plus 1 into my kvs before playing the song so I will mark this thread as solved.

B4X:
kvs.Put("CurrentSongSelected", Index +1)
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Since I now know what the index is I will skip the selecting of the CLV item. I just wanted to simulate tapping that item first but the ultimate part is getting the index value plus 1 into my kvs before playing the song so I will mark this thread as solved.

B4X:
kvs.Put("CurrentSongSelected", Index +1)
I just started to use the CLVSelections class by Erel and it has this code that does just what I wanted. It selects the CLV item and that made the visual selecting effect as well.

B4X:
    CSelections.ItemClicked(Index)
 
Upvote 0
Top