How To Cycle Through Panels of a Tabhost And Reset All Edittext Views

Mahares

Expert
Licensed User
Longtime User
I have a tabhost that has 6 panels that were intitialized. They all contain several EDITTEXT views that I would like to reset to BLANK after a button is tapped to save a record to a table. I can blank all EDITTEXT views if I write the same code 6 times, one for each panel. But, I would like to cycle through all panels programmatically as soon as the record is saved. Below is the code I tried to iterate through all 6 panels, but it crashes at this line:For i = 0 To MyPanel.NumberOfViews - 1


Dim MyPanelList As List
Dim i, j As Int
Dim MyPanel As String
MyPanelList.Initialize()
MyPanelList.AddAll(Array As String("Panel1","Panel2","Panel3","Panel4","Panel5","Panel6"))
For j=0 To MyPanelList.Size-1
MyPanel=MyPanelList.Get(j)
For i = 0 To MyPanel.NumberOfViews - 1
If MyPanel.GetView(i) Is EditText Then
Dim MyViewETReset As EditText
MyViewETReset = MyPanel.GetView(i)
MyViewETReset.Text=""
End If
Next
Next
 

klaus

Expert
Licensed User
Longtime User
Try to change the two lines below:
B4X:
Dim MyPanel As String
MyPanelList.Initialize()
MyPanelList.AddAll(Array As String("Panel1","Panel2","Panel3","Panel4","Panel5  ","Panel6"))
to
B4X:
Dim MyPanel As Panel
MyPanelList.Initialize()
MyPanelList.AddAll(Array As Panel(Panel1, Panel2, Panel3, Panel4, Panel5, Panel6))
Best regards.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I changed the 2 lines you advised to change. Now it crashes at this line after it is deployed to the device:
MyPanel=MyPanelList.Get(j)

When I hover the mouse cursor on the code in the IDE PC window on that line that returned the error, the message displays: BAlayout: Layout not available.
I do not have any layouts in my code called BAlayout or any deleted with that name.
I appreciate a follow-up.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Let me see how I can create a smaller and cleaner project so as not to confuse you further. In the meantime, I can still successfully create a smaller procedure for each of the 6 panels individually. it is not a big deal. We are not talking about a great number of lines. If there is a similar process in the forum that I can refer to that resembles my situation, I can research it further, but I have not found one.
Thank you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I have looked closer at your problem.
Setting a List of Panels thoughs an error when trying to get an item.
It seems that the system doesn't recognize Panels because they aren't native views but BALayouts.

You can use a simple Array of Panels.

The code below does work.
B4X:
Sub Globals
    Dim TabHost1 As TabHost
    Dim Panel1, Panel2 As Panel
    Dim Panels(2) As Panel
    Dim btnClear As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim i, j As Int
    
    TabHost1.Initialize("TabHost1")
    Activity.AddView(TabHost1, 0, 0, 100%x, 100%y)
    
    btnClear.Initialize("btnClear")
    Activity.AddView(btnClear, 50dip, 350dip, 100dip, 50dip)
    btnClear.Text = "Clear"
    
    For i = 0 To 1
        Panels(i).Initialize("")
        For j = 0 To 3
            Dim edt As EditText
            edt.Initialize("edt")
            edt.Tag = "Test" & i & j
            edt.Text = "Test" & i & j
            Panels(i).AddView(edt, 10dip, 10dip + j * 60dip, 150dip, 50dip)
        Next
        TabHost1.AddTab2("Test" & i,Panels(i))
    Next
End Sub

Sub btnClear_Click
    Dim i, j As Int
    
    For i = 0 To 1
        For j = 0 To Panels(i).NumberOfViews - 1
            Dim edt As EditText
            edt = Panels(i).GetView(j)
            edt.Text = ""
        Next
    Next
End Sub
Best regatds.
 

Attachments

  • TabHostPanelAccess.zip
    5.6 KB · Views: 193
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Because all my 6 panels had concrete names and all were declared as follows, I could not get the code snippet to work, even when I replaced my panels names using Panels(0), Panels(1), etc.

Sub Globals
Dim TabHost1 As TabHost
Dim pnlPanelGas,pnlPanelPressures ,pnlPanelShip As Panel
Dim pnlPanelConsumables,pnlPanelFluids,pnlPanelComments As Panel
---------------------------------------------------------------
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("LayoutMain")
pnlPanelGas.Initialize("")
pnlPanelPressures.Initialize("")
pnlPanelConsumables.Initialize("")
pnlPanelFluids.Initialize("")
pnlPanelShip.Initialize("")
pnlPanelComments.Initialize("")
TabHost1.AddTab2("GAS", pnlPanelGas)
TabHost1.AddTab2("PRES", pnlPanelPressures)
TabHost1.AddTab2("CONSUM", pnlPanelConsumables)
TabHost1.AddTab2("FLUID", pnlPanelFluids)
TabHost1.AddTab2("SHIP", pnlPanelShip)
TabHost1.AddTab2("COMNT", pnlPanelComments)

pnlPanelGas.LoadLayout("LayoutGas")
pnlPanelPressures.LoadLayout("LayoutPressures")
pnlPanelPressures.LoadLayout("LayoutPressures")pnlPanelConsumables.LoadLayout("LayoutConsumables")pnlPanelFluids.LoadLayout("LayoutFluids")
pnlPanelShip.LoadLayout("LayoutShip")
pnlPanelComments.LoadLayout("LayoutComments")

-------------------------------------------------------------------
Dim i, j As Int 'Dimed in globals
Dim Panels(6) 'Dimed in globals
For i = 0 To 1
For j = 0 To Panels(i).NumberOfViews - 1
Dim edt As EditText
edt = Panels(i).GetView(j)
edt.Text = ""
Next
Next

----------------------------------------------------------------------
The line that returned the same error as before is this: edt = Panels(i).GetView(j)
even with the following changes in my code to match yours:


Dim i, j As Int 'Dimed in globals
Dim Panels(6) 'Dimed in globals

For i = 0 To 5
Panels(i).Initialize("")
Next
TabHost1.AddTab2("GAS", Panels(0)) 'create a panel in 1st page
TabHost1.AddTab2("PRES", Panels(1)) 'create a panel in 2nd page
TabHost1.AddTab2("CONSUM", Panels(2)) 'create a panel in 3rd page
TabHost1.AddTab2("FLUID", Panels(3)) 'create a panel in 4th page
TabHost1.AddTab2("SHIP", Panels(4)) 'create a panel in 5th page
TabHost1.AddTab2("COMNT", Panels(5)) 'create a panel in 6th page

Panels(0).LoadLayout("LayoutGas") 'Load named layout to Panels(0) panel
Panels(1).LoadLayout("LayoutPressures") 'Load named layout to Panels(1) panel
Panels(2).LoadLayout("LayoutConsumables") 'Load named layout to Panels(2)
Panels(3).LoadLayout("LayoutFluids") 'Load named layout to Panels(3)
Panels(4).LoadLayout("LayoutShip") 'Load named layout to Panels(4)
Panels(5).LoadLayout("LayoutComments") 'Load named layout to Panels(5)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…