Android Question manipulating panels from an array

67biscuits

Member
I am looking for a way to manipulate the visible status of panels by using an array (list) in order to do so.
I have tried this but the app crashes:
B4X:
Dim pnls As List = Array As Panel(pnlClient, pnlTime, pnlMtrl, pnlEquip, pnlTasks, pnlNotes)

sub buttonPushed
      Dim b As Button = Sender
     Dim t As Int = b.Tag
           Log(pnls.Size)  'puts the number 6 on the log screen
     For x = 0 To pnls.Size - 1
        pnls.Get(x).As (Panel).Visible = False
    Next
end sub

Any help would be appreciated
 

67biscuits

Member
I've copied it all in back in to demonstrate and to include the error msg.


declared sub class_globals:
B4X:
    Dim pnls As List = Array As Panel(pnlClient, pnlTime, pnlMtrl, pnlEquip, pnlTasks, pnlNotes)
in a button activated Sub:
B4X:
    Dim b As Button = Sender
    Dim t As Int = b.Tag
    
    For x = 0 To pnls.Size - 1
        pnls.Get(x).As(Panel).Visible = False
    Next
    pnls.get(t).as(Panel).visible = True

** Activity (main) Create (first time) **
** Activity (main) Resume **
Error occurred on line: 321 (B4XMainPage)
java.lang.ClassCastException: anywheresoftware.b4a.objects.PanelWrapper cannot be cast to android.view.ViewGroup
at b4a.example.b4xmainpage._openpanel_click(b4xmainpage.java:956)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:209)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28309)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7701)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:610)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try this
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private pnlClient As B4XView
    Private pnlTime As B4XView
    Private pnlMtrl As B4XView
    Private pnlEquip As B4XView
    Private pnlTasks As B4XView
    Private pnlNotes As B4XView
    Private pnls As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    pnls.Initialize
    pnls.AddAll(Array(pnlClient, pnlTime, pnlMtrl, pnlEquip, pnlTasks, pnlNotes))
End Sub

Sub Button1_Click
    For x = 0 To pnls.Size -1
        pnls.Get(x).As(Panel).Visible = False
    Next
End Sub
Eventually post your full code (it would be better a project to test with the issue) to check what it's not working.
Or use an ARRAY instead of a List if you have a small fixed number of item like in your case
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private pnlClient As B4XView
    Private pnlTime As B4XView
    Private pnlMtrl As B4XView
    Private pnlEquip As B4XView
    Private pnlTasks As B4XView
    Private pnlNotes As B4XView
    Private pnls() As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    pnls = Array as Panel(pnlClient, pnlTime, pnlMtrl, pnlEquip, pnlTasks, pnlNotes)
End Sub

Sub Button1_Click
    For x = 0 To pnls.Length - 1
        pnls(x).Visible = False
    Next
End Sub
 
Upvote 0

67biscuits

Member
Except for a detail here and there I have essentially have your first example. i will double check the details and fix and retry. The main difference being I dim the panels as panels as opposed to B4Xview. I have far less issues and program for my own devices exclusively.
Thanks for the help Sagenut. I adjusted the details in your first example. (if I'm right, its just the way I used AddAll assembling the pnls list. Anyway, it works, and I will study it a little more to grasp a better understanding. thanks so much
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Also, may I add... being this a Question, why did the OP taged it as "Code Snippet"?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
(taken from the post back a few posts...)
java.lang.ClassCastException: anywheresoftware.b4a.objects.PanelWrapper cannot be cast to android.view.ViewGroup

I got it when I made the pnls[] elements invisible. More aptly, tried to.
I think that this happen because you set the array in this way
B4X:
Array as Panel(.......)
Try setting it with
B4X:
Array(.......)
Did you test my code example?
 
Upvote 0
Top