Android Question CustomListView.RemoveAt does not remove all selected rows

mfstuart

Active Member
Licensed User
Longtime User
I'm testing a part of my app to allow the user to LongClick on a CLV. The code in turn would add the Index to a List.
The user can then select another CLV row and it does the same, adds the Index to a List.

In the CLV LongClick code, my ActionBars (Panels) are manipulated to hide the "main bar" and show the "delete bar". They are panels with other views on them, like Labels and Buttons.

On the "delete bar" there is a Button - btnDelete. When pressed it cycles thru the List of Indexes and runs the CLV.RemoveAt(Index) code.
Here's the issue: the code only removes the first Index in the List and not the others. That is of course if the user selected more than one row in the CLV.
The zipped project is attached.

The btnDelete code is not working as desired.
Does the CLV.RemoveAt(Index) re-index the CLV indexes?

This code is for the "Default" type of B4A project:

Activity_Create:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    SelectedItems.Initialize
    Load_Data
End Sub

Load_Data:
Sub Load_Data
    clvItems.Clear
    For i = 0 To 20
        clvItems.AddTextItem("Item " & i,i)
    Next
End Sub

LongClick:
Private Sub clvItems_ItemLongClick(Index As Int, Value As Object)
    pnlActionBar.Visible = False
    pnlDeleteBar.Visible = True
    Dim p As Panel = clvItems.GetPanel(Index)
    p.Color = xui.Color_LightGray
    SelectedItems.Add(Index)
    lblDeleteTitle.Text = "Delete " & SelectedItems.Size
End Sub

btnDelete_Click:
Private Sub btnDelete_Click
    Dim ItemCount As Int = SelectedItems.Size        '<=== SelectedItems is a List
    Log("ItemCount: " & ItemCount)
    
    If ItemCount > 0 Then
        For i = 0 To ItemCount - 1
            Dim idx As Int = SelectedItems.Get(i)
            Log(idx)
            clvItems.RemoveAt(idx)    '<=== should remove all items in List, BUT DOES NOT!
        Next
        ToastMessageShow(ItemCount & " items deleted.", False)
    Else
        ToastMessageShow("No Items selected!", False)
    End If
End Sub
 

Attachments

  • LongTouch_Delete_Demo.zip
    10.6 KB · Views: 66

mfstuart

Active Member
Licensed User
Longtime User
Mahares, I'm looking into that issue of "undoing" a selection.

Functionality:
If there are more than one row selected, and a row is de-selected, keep showing the "Delete Bar".
If there is only 1 row left and it is unselected, then hide the "Delete Bar" and show the "Main Bar".

Thanx,
Mark Stuart
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Try this code. It uses the Panel Color as the "IsSelected" pseudo property.
This still uses the SelectedItems List object to ascertain whether there are any rows selected to show or hide the "Delete Bar".
You have to use the SelectedItems.IndexOf(Index) to get the real index in the List object. See in the code how it is used. It works!

Hope it works for y'all. Let me know how it goes. I'd like to mark this thread as SOLVED.
clvItems_ItemLongClick:
Private Sub clvItems_ItemLongClick(Index As Int, Value As Object)
    Dim p As Panel = clvItems.GetPanel(Index)
    Dim cnv As Canvas
    cnv.Initialize(p)
    Dim clr As Int = cnv.Bitmap.GetPixel(0,0)
    SelectedItems.Sort(False)
    
    If clr = 0 Then
        p.Color = xui.Color_LightGray
        SelectedItems.Add(Index)
    Else
        p.Color = 0
        Dim idx As Int = SelectedItems.IndexOf(Index)
        SelectedItems.RemoveAt(idx)
    End If
    
    lblDeleteTitle.Text = "Delete " & SelectedItems.Size
    
    If SelectedItems.Size <> 0 Then
        pnlActionBar.Visible = False
        pnlDeleteBar.Visible = True
    Else
        pnlActionBar.Visible = True
        pnlDeleteBar.Visible = False
    End If
End Sub
 
Upvote 0
Top