Android Question Remove Dynamica Panels

Mitesh_Shah

Member
Licensed User
Longtime User
Hi
how delete dynamically added panel
have 2 button 1 for add new panel & 1 for remove panel with its contain data

we add panel dynamically, each panel has one delete check box to select and delete with button

here i attached my code and project folder
B4X:
Private Sub add_button_Click
    FrameCounter = FrameCounter + 1 ' Increment the frame counter

    Dim NewPanel As B4XView = xui.CreatePanel("")

    NewPanel.Color = xui.Color_ARGB(255, 224, 224, 235)        ' Set a background color for the frame
    
    MainScroll.Panel.AddView(NewPanel, 15dip, 100dip + (FrameCounter - 1) * 150dip, 92%x, 140dip) ' Dynamically position each frame
    

    Dim Newlable As Label
    Dim DLY_lable As Label   
    
    ' Add checkboxes dynamically
    Dim i As Int
    Dim chkList As List
    chkList.Initialize
    
    For i = 0 To 6
        Dim chk As CheckBox
        chk.Initialize("chk_" & FrameCounter & "_" & i) ' Unique ID for each checkbox
        chk.Text = i
        chk.Tag = FrameCounter & "_" & i ' Store panel and checkbox index
        
        NewPanel.AddView(chk, 10dip + (i * 60dip), 20dip, 100dip, 20dip)        '// horizontal
        chk.Tag = i & "_" & FrameCounter
        chkList.Add(chk)
    Next
            
    Newlable.Initialize("")
    Newlable.Color = xui.Color_ARGB(255, 224, 224, 235)
    Newlable.TextColor = Colors.red
    Newlable.TextSize = 20
    NewPanel.AddView(Newlable, 10dip, 50dip, 70%x, 40dip) ' Position inside the panel
    Newlable.Text =  "Time 12 : 30"
    
    DLY_lable.Initialize("")
    'Newlable.Color = Colors.White
    DLY_lable.Color = xui.Color_ARGB(255, 224, 224, 235)
    DLY_lable.TextColor = Colors.red
    DLY_lable.TextSize = 20
    NewPanel.AddView(DLY_lable, 10dip, 100dip, 70%x, 40dip) ' Position inside the panel
    DLY_lable.Text =  "Cycle  On: 10 S - 20 S"
    
    ' Create a Button inside the frame
    Dim NewButton As Button
    NewButton.Initialize("DynamicButton") ' Set event name for click events
    NewButton.Text ="Edit"
    NewPanel.AddView(NewButton, 360dip, 90dip, 15%x, 40dip)
    
    ' Add checkbox
    Dim chk As CheckBox
    chk.Initialize("")
    chk.Tag = "DEL_" & NewPanel.Tag ' Store panel tag for reference
    chk.Text = "DEL"
    NewPanel.AddView(chk, 280dip, 90dip, 15%x, 40dip)

    ' Attach the Frame ID to the button's Tag for identification
    NewButton.Tag = FrameCounter
    
    ' Store the panel and its checkboxes
    panelList.Add(Array As Object(NewPanel, 0, panelList.Size + 1))
        
    MainScroll.Panel.Height = Max(MainScroll.Height, 10dip + FrameCounter * 150dip)
End Sub

Private Sub del_Button_Click

    Dim toRemove As List
    toRemove.Initialize
    
    Dim panelData() As Object = panelList.Get(0)
    Dim pnl As B4XView = panelData(0)
    
    Log(pnl)

'    For Each pnl As B4XView In panelList
'        Dim chk As CheckBox = pnl.GetView(0) ' First view is checkbox
'        Dim i As  B4XView = pnl.GetView(0) ' First view is checkbox       
'        Log(i.Text)
        'If chk.Checked Then
        '    toRemove.Add(pnl)
        'End If
'    Next

    ' Remove panels
'    For Each pnl As B4XView In toRemove
        'pnlContainer.RemoveView(pnl)       
        'panelList.Remove(pnl)
        
'        pnlContainer.RemoveViewFromParent
'        panelList.RemoveAt(pnl)
'    Next
End Sub
 

Attachments

  • add_delet selected panel.jpg
    add_delet selected panel.jpg
    168.5 KB · Views: 111
  • Add_Remove_Panel.zip
    13.2 KB · Views: 96

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't think that switching to a CustomList will, by itself, answer your question. Looking at your code you seem to be planning to remove deleted panels view-by-view. That is not the way to handle this situation in my view. It is much easier to redraw all the panels in the scrollview; simply leave out the deleted panel. This means that you have to keep a record of the panel states (which checkboxes are checked; the value in each text box etc) somewhere external to the panels themselves, but you are more or less doing that already. I create a List of Type objects to do this - it is a better way to manage data than to have it distributed across a lot of views. When you delete a panel its corresponding data structure is removed from the list and the remaining panel set is redrawn.
 
Upvote 0

Mitesh_Shah

Member
Licensed User
Longtime User
I don't think that switching to a CustomList will, by itself, answer your question. Looking at your code you seem to be planning to remove deleted panels view-by-view. That is not the way to handle this situation in my view. It is much easier to redraw all the panels in the scrollview; simply leave out the deleted panel. This means that you have to keep a record of the panel states (which checkboxes are checked; the value in each text box etc) somewhere external to the panels themselves, but you are more or less doing that already. I create a List of Type objects to do this - it is a better way to manage data than to have it distributed across a lot of views. When you delete a panel its corresponding data structure is removed from the list and the remaining panel set is redrawn.
OK
 
Upvote 0

Mitesh_Shah

Member
Licensed User
Longtime User
I will say it again.
It is much easier with CustomListView.
Hi sir its possible to create this type layout with CustomListView ?

three is various timer schedules, its add bye user

also its edit or delete option
 

Attachments

  • Timer_App_5.jpg
    Timer_App_5.jpg
    39.4 KB · Views: 106
  • Timer_App_4.jpg
    Timer_App_4.jpg
    34.4 KB · Views: 104
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Hi sir its possible to create this type layout with CustomListView ?
I would also use a customlistview with so many elements, as not every device is so large and therefore has to be scrolled on smaller devices.
 
Upvote 0
Top