Android Question How can I delete an index in CustomListView by clicking on a view ?

SMOOTSARA

Active Member
Licensed User
Longtime User
Hi

What is the correct way to delete items in the list?

I have prepared the following version but it does not work well
Each time I delete an option, the indexes change

thank you

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

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

'#BridgeLogger: True

Sub Process_Globals
    Public ActionBarHomeClicked As Boolean
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub

'Template version: B4A-1.01
#Region Delegates

Sub Activity_ActionBarHomeClick
    ActionBarHomeClicked = True
    B4XPages.Delegate.Activity_ActionBarHomeClick
    ActionBarHomeClicked = False
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
    B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Sub Create_Menu (Menu As Object)
    B4XPages.Delegate.Create_Menu(Menu)
End Sub

#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;
    
}
#End If
#End Region

'Program code should go into B4XMainPage and other pages.



B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim PageClv1 As PageClv
    
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    
    PageClv1.Initialize
    B4XPages.AddPage("PageClv1",PageClv1)
    
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    B4XPages.ShowPage("PageClv1")
End Sub


B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    
    Private CustomListView1 As CustomListView

    
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("LayoutPageClv")
    For i=0 To 99
        
        Dim XCustomViewItem1 As XCustomViewItem
        XCustomViewItem1.Initialize(Me,"XCustomViewItem1")
        
        XCustomViewItem1.DesignerCreateView(CustomListView1.GetBase.Width,i)
        CustomListView1.Add(XCustomViewItem1.mBase ,i)
        
    Next
    
    
    
    
End Sub


Sub Remove_XCustomViewItem1 (i As Int)
    Log("del index => "& i)
    CustomListView1.RemoveAt(i)
End Sub

B4X:
#DesignerProperty: Key: BooleanExample, DisplayName: Show Seconds, FieldType: Boolean, DefaultValue: True
#DesignerProperty: Key: TextColor, DisplayName: Text Color, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: Text color

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
    
    Dim LabelDel As Label
    Dim LabelDel2 As Label
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
    
    Dim mBase As B4XView = xui.CreatePanel("")
    
End Sub

'Base type must be Object
'Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
Public Sub DesignerCreateView (WidthView As Int,index As Int)
    
    
'    mBase = Base
    mBase.Color=Colors.ARGB(255,Rnd(0,255),Rnd(0,255),Rnd(0,255))
    
    mBase.Width=WidthView
    mBase.Height=25%y
    
'    Tag = mBase.Tag
'    mBase.Tag = Me
'      Dim clr As Int = xui.PaintOrColorToColor(Props.Get("TextColor")) 'Example of getting a color value from Props


    LabelDel.Initialize("LabelDel")
    LabelDel.Tag=index
    LabelDel.Text="Click Del index="&index
    LabelDel.Gravity=Bit.Or(Gravity.CENTER_HORIZONTAL,Gravity.CENTER_VERTICAL)
    LabelDel.Color=Colors.Red
    mBase.AddView(LabelDel,5%x,5%y,30%x,10%y)
    
    LabelDel2.Initialize("LabelDel2")
    LabelDel2.Tag=index
    LabelDel2.Text=index
    LabelDel2.Gravity=Bit.Or(Gravity.CENTER_HORIZONTAL,Gravity.CENTER_VERTICAL)
    LabelDel2.Color=Colors.Yellow
    mBase.AddView(LabelDel2,0%x,0%y,5%x,5%x)


End Sub



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


Sub LabelDel_Click
    Log("who to delete this index from Clv?")
    
    Dim b As Label
    b = Sender
    b.Color=Colors.Yellow
    Log(b.Tag)
    b.Text = "who to delete this index from Clv?"
    CallSub2(mCallBack,"Remove_"&mEventName,b.Tag)
End Sub


1646239451549.png
 

Attachments

  • Project.zip
    16.5 KB · Views: 79

LucaMs

Expert
Licensed User
Longtime User
Dim XCustomViewItem1 As XCustomViewItem
XCustomViewItem1.Initialize(Me,"XCustomViewItem1")

XCustomViewItem1.DesignerCreateView(CustomListView1.GetBase.Width,i)
CustomListView1.Add(XCustomViewItem1.mBase ,i)
That's not the right way to add items to a CustomListView:
https://www.b4x.com/android/forum/t...listview-cross-platform-customlistview.84501/

I have prepared the following version but it does not work well
Each time I delete an option, the indexes change
Don't base what you want to do on the index of items; what do you want to achieve?
You can/should use the value or tag associated with the item.
 
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
That's not the right way to add items to a CustomListView:
https://www.b4x.com/android/forum/t...listview-cross-platform-customlistview.84501/


Don't base what you want to do on the index of items; what do you want to achieve?
You can/should use the value or tag associated with the item.
Ciao Luca

I am thankful with your guidance (Grazie) 😁
Your method is good, but it is used when you create a view inside the class.

I have to add a customview to the list from the outside(from layout or xcustomview)
And when I click on the delete button, I delete it index from clv.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Ciao Luca

I am thankful with your guidance (Grazie) 😁
Your method is good, but it is used when you create a view inside the class.

I have to add a customview to the list from the outside(from layout or xcustomview)
And when I click on the delete button, I delete it index from clv.
Se avessi posto la domanda nel forum italiano, ti avrei capito meglio... forse 😄
If you had asked the question in the Italian forum, I would have understood you better... maybe 😄

They are two distinct problems (actually, I created the second one, the way to add items to CLV, but it was... "necessary" 😁).

You have created a custom view that represents the whole item. If you "necessarily" want to do this, create an item layout anyway and put your custom view inside it, in the designer, of course, and then load the layout as I did in the example I attached.

Each time I delete an option, the indexes change
What do you mean? If you delete an item, there will no longer be an item with that index, that is, the "hole is not plugged"; this is how it works (see the project attached).
 

Attachments

  • ProjectLm2.zip
    16.8 KB · Views: 79
Last edited:
Upvote 0
Top