Android Question Better/Correct Coding

Declan

Well-Known Member
Licensed User
Longtime User
Is there a better / correct way to do this:
Code: Copy
NumZones = Curs.RowCount
lblTotalZones.Text = NumZones

If NumZones = 1 Then
    pnlZone1.Visible = True
End If
If NumZones = 2 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
End If
If NumZones = 3 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
End If
If NumZones = 4 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
End If
If NumZones = 5 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
End If
If NumZones = 6 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
End If
If NumZones = 7 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
End If
If NumZones = 8 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
    pnlZone8.Visible = True
End If
If NumZones = 9 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
    pnlZone8.Visible = True
    pnlZone9.Visible = True
End If
If NumZones = 10 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
    pnlZone8.Visible = True
    pnlZone9.Visible = True
    pnlZone10.Visible = True
End If
If NumZones = 11 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
    pnlZone8.Visible = True
    pnlZone9.Visible = True
    pnlZone10.Visible = True
    pnlZone11.Visible = True
End If
If NumZones = 12 Then
    pnlZone1.Visible = True
    pnlZone2.Visible = True
    pnlZone3.Visible = True
    pnlZone4.Visible = True
    pnlZone5.Visible = True
    pnlZone6.Visible = True
    pnlZone7.Visible = True
    pnlZone8.Visible = True
    pnlZone9.Visible = True
    pnlZone10.Visible = True
    pnlZone11.Visible = True
    pnlZone12.Visible = True
End If

I can have up to 144 Zones - so the above seems crazy :mad:
 
being picky ...
Code: Copy
For i = 0 To numZones -1
 
Upvote 0
When I run:
Code: Copy
NumZones = Curs.RowCount
lblTotalZones.Text = NumZones

Dim pnlZone(12) As Panel

For i = 0 To NumZones -1
    pnlZone(i).Visible = True
Next

I get an error:
java.lang.RuntimeException: Object should first be initialized (Panel).
 
Upvote 0
than you have to add them to a list, because you will not have an array when you define them with the designer....
something like this - not tested, just an idea
Code: Copy
    Dim pnlZone As List
    pnlZone.Initialize2(Array As Panel(pnlZone1, pnlZone2,pnlZone3...) 
    For i = 0 To NumZones -1
      Dim p As Panel
      p = pnlZone.get(i)
      p.Visible = True
    Next
 
Upvote 0
than you have to add them to a list, because you will not have an array when you define them with the designer....
something like this - not tested, just an idea
Code: Copy
    Dim pnlZone As List
    pnlZone.Initialize2(Array As Panel(pnlZone1, pnlZone2,pnlZone3...)
    For i = 0 To NumZones -1
      Dim p As Panel
      p = pnlZone.get(i)
      p.Visible = True

    Next
If I run the above - edited: pnlZone.Initialize2(Array As Panel(pnlZone1, pnlZone2,pnlZone3) - for only 3 Panels
I get the following error:
ClassCastException:
anywheresoftware.b4a.objects.PanelWrapper cannot be cast to android.view.ViewGroup
 
Upvote 0
what about this? (main layout holds the 3 panels)

Code: Copy
Sub Globals
Dim Panel1,Panel2,Panel3 As Panel
Dim numZones As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
Dim pnlZones() As Panel=Array As Panel(Panel1,Panel2,Panel3)
numZones=2
For i = 0 To pnlZones.Length-1
If i<numZones Then
  pnlZones(i).Visible = True
Else
  pnlZones(i).Visible = False
End If
Next
End Sub
 
Upvote 0
all is OK - just proves Panels are initialized
Code: Copy
Dim pnlZone(12) As Panel

For i = 0 To NumZones -1
    pnlZone(i).Visible = True
Next

You need to manually add the panels to this array after loading the layout and before you can use them....
 
Upvote 0
Code: Copy
Dim pnlZone(12) As Panel

For i = 0 To NumZones -1
    pnlZone(i).Visible = True
Next

You need to manually add the panels to this array after loading the layout and before you can use them....

I don't understand - how would I manually add the panels?
I have one large Panel that I use as a "Frame", this "Frame" then has a TabHost view that has 12 panels that are "pnlZone1" through "pnlZone12". This TabHost will have 6 tabs, each with 12 panels giving a total of 72 pnlZone panels.
I am only using 1 Layout in my app that has multiple Panels that I make .Visible=True/False depending on the current function.
 
Upvote 0
you can do it by code aswell.

but the main question is if this design is good for your app?

you could work with maximum 6 panels aswell which act as paging panels that get updated based on the tab/button selection.
 
Upvote 0
Thanks all - This worked:
Code: Copy
    numZones = Curs.RowCount
    Dim pnlZones() As Panel=Array As Panel(pnlZone1,pnlZone2,pnlZone3,pnlZone4,pnlZone5,pnlZone6, _
    pnlZone7,pnlZone8,pnlZone9, pnlZone10,pnlZone11,pnlZone12)
    For i = 0 To pnlZones.Length-1
    If i<numZones Then
      pnlZones(i).Visible = True
    Else
      pnlZones(i).Visible = False
    End If
    Next

More important - I now understand HOW IT WORKS
 
Upvote 0
you can do it by code aswell.

but the main question is if this design is good for your app?

you could work with maximum 6 panels aswell which act as paging panels that get updated based on the tab/button selection.

I have a TabHost view that will have 6 "Tabs". Each "Tab" will have 12 pnlZones.
The user will then be able to page through all zones by selecting the Tabs of the TabHost.

My question is:
1). How do I add a Tab and text to my existing TabHost (created in Designer)
I tried:
Code: Copy
tabZones.AddTab("Zones 1 - 12", "LayoutMain")
But nada.

2). As each tab has 12 panels. How would I add a new tab when I have 13 to 24 panels?
 
Upvote 0
Back
Top