Android Question Remove item from CustomListView

red30

Well-Known Member
Licensed User
Longtime User
As with ImageView1_Click to remove the selected item from CustomListView?
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private Chooser As ContentChooser
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    Private MyCLV As CustomListView
    Private Button1 As Button
    Private CLV_IV As CustomListView
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    Chooser.Initialize("chooser")
    For i = 1 To 10
        MyCLV.Add(CreateListItem("MyText"&i),i)
    Next
End Sub

Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Button1.Tag = CLV_IV
    Return p
End Sub

Sub CreateListItem2(btm As Bitmap) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100dip, 100dip)
    p.LoadLayout("Item_IV")
    ImageView1.Bitmap=btm
    ImageView1.Tag=CLV_IV
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim btn As Button = Sender
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Log("Checked items: " & index)
    Chooser.Show("image/*", "Choose image")
    Wait For chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim clv As CustomListView = btn.Tag
        clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
    End If
End Sub

Sub ImageView1_Click
    Dim iv As ImageView = Sender
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    
    Log("Checked items: " & index)
End Sub
 

red30

Well-Known Member
Licensed User
Longtime User
You do not understand. Look at the video, I need to delete one photo (ImageView1), to which I clicked.
B4X:
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    MyCLV.RemoveAt(index)
Delete all. I have two CustomListView. I need to delete a photo from CLV_IV.
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
If you are using CLV V1.52 and above, then CLV.RemoveAt is working correctly, thus you can remove just that one row/line.

Are you saying you wish to clear an image or remove the whole row, they are two completely different questions?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
If you are using CLV V1.52 and above, then CLV.RemoveAt is working correctly, thus you can remove just that one row/line.

Are you saying you wish to clear an image or remove the whole row, they are two completely different questions?
I want to delete one ImageView from CustomListView. But I in MyCLV have one more CLV_IV, and in it already ImageViev.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
Dim iv As ImageView = Sender

iv.RemoveView
This is an example of working and deleting how I want it to be. But still I have a question. How can I remove an element (that contains an image) of CustomListView?
I have CustomListView ("MyCLV"). It consists of 10 elements: Button1 + CLV_IV (horisontal CustomListView). CustomListView ("CLV_IV") contains ImageView1. I add a new element containing an image to CLV_IV with:
B4X:
Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Button1.Tag = CLV_IV
    Return p
End Sub

Sub Button1_Click
    Dim btn As Button = Sender
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Log("Checked items: " & index)
    Chooser.Show("image/*", "Choose image")
    Wait For chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim clv As CustomListView = btn.Tag
        clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
    End If
End Sub
Clicking this image I want to remove it as an element of CLV_IV. What I have now: I add several images, after I push on some of them and they are deleted BUT it leaves some ugly gaps = empty space. As on the video. How to fix it? I think that it needs to remove an ELEMENT of CLV_IV but not just an image but also can not do it.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Dim iv As ImageView = Sender

iv.RemoveView

That's not the right way to do it. Your removing the image view outside of the CLV. How is the CLV supposed to know that an item belonging to it was removed? The issue is that when you "click" on an object of a nested CLV, you have to work yourself down to that CLV that is associated with the clicked object. This is a little tricky because you then need to know which CLV is the next one. It be really worthwhile to watch @Erel's video on CLV (found here: https://www.b4x.com/etp.html). The modifications I made to your code sample include:

1) Adding a custom type that allows setting and retrieving of the second CLV (Technically could have just used a CustomListView object. I'm just allowing for future expansion, and I'm following along with the video. My first rodeo with CLV's)
2) Modifying MyCLV.Add to include this custom type
3) Modifying CreateListItem to include this custom type
4) Modifying both CreateListItem and CreateListItem to use B4XView objects (this was technically not necessary. Again, I'm just following the video.)
5) Modifying ImageView_Click to properly retrieve the underlying CLV and remove the item
 

Attachments

  • CLVinCLV.zip
    16.1 KB · Views: 292
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…