Android Question Multi-level Menu with listbox

Rajesh kannan MJ

Member
Licensed User
Longtime User
Hi,

I needed a multi level menu. I could not find one. May be I might have missed too.

After creating my own data structure, I created a multi-level menu with backbutton (not hardware) feature,

But I have a question. Are there any better optimized ways to do it? I struggled a lot to create tree node strucutre based data type. And I thought I better could create one like this,

Video demo here


The code is inside the Spoiler,

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

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim mymenuList As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ListView1 As ListView
    Private btnBack As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("lyt1") 
    mymenuList.Initialize
   
    'THE MENU STRUCTURE
    'GroupID, ParentID, Link Text, Link URL, Child ID / empty string
    mymenuList.Add(Array As String("0","-1","Vinayagar >","","2"))
    mymenuList.Add(Array As String("0","-1","Murugan","#link2",""))
    mymenuList.Add(Array As String("0","-1","Shiva >","","1"))
    mymenuList.Add(Array As String("0","-1","Other Songs","#link3",""))
    mymenuList.Add(Array As String("0","-1","Menu Level 1 >","","3"))
   
    mymenuList.Add(Array As String("1","0","Shiva song 1","#link4",""))
    mymenuList.Add(Array As String("1","0","Shiva song 2","#link5",""))
       
    mymenuList.Add(Array As String("2","0","Vinayagar song 1","#link",""))
    mymenuList.Add(Array As String("2","0","Vinayagar song 2","#link",""))
   
    mymenuList.Add(Array As String("3","0","Menu Level 2 >","","4"))
   
    mymenuList.Add(Array As String("4","3","Song","#",""))
   
    FillTheListBox("0")
   
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim parseValue() As String
    Dim childId As String
    Dim idToFill As String

    parseValue = Value
   
    childId = parseValue(4)
    Log(childId & "Child ID")
    idToFill = childId
   
    'clear list only if the groupid is not empty
    If childId == "" Then
        'Link is available and work with that link
        Log("a link clicked from list view" & parseValue(3))
    Else
        FillTheListBox(idToFill)
    End If   
       
End Sub

Sub backButtonPressed
    If ListView1.Tag = "-1" Then
        Log("You are on root menu")
    Else
        FillTheListBox(ListView1.Tag)
    End If
End Sub

Sub btnBack_Click
    'Log("Return to the id " & ListView1.Tag)
    backButtonPressed
End Sub

Sub FillTheListBox(groupID As String)
    Dim eachItemMenu() As String
    'clear the list
    ListView1.clear
    For i = 0 To mymenuList.Size - 1
        Log(groupID)
        eachItemMenu = mymenuList.Get(i)
        If eachItemMenu(0) == groupID Then
            ListView1.AddSingleLine2(eachItemMenu(2),eachItemMenu)
            ListView1.Tag = eachItemMenu(1)
        End If
    Next
End Sub


The menu structure I used is,

It is an array which includes, first string Group ID or can be termed as level id
The second value is "parent id" -1 means root
Third one is "Link Text"
Fourth one is "Link"
Fifth is Child id

So, everything is related

B4X:
'THE MENU STRUCTURE
    'GroupID, ParentID, Link Text, Link URL, Child ID / empty string
    mymenuList.Add(Array As String("0","-1","Vinayagar >","","2"))
    mymenuList.Add(Array As String("0","-1","Murugan","#link2",""))
    mymenuList.Add(Array As String("0","-1","Shiva >","","1"))
    mymenuList.Add(Array As String("0","-1","Other Songs","#link3",""))
    mymenuList.Add(Array As String("0","-1","Menu Level 1 >","","3"))
  
    mymenuList.Add(Array As String("1","0","Shiva song 1","#link4",""))
    mymenuList.Add(Array As String("1","0","Shiva song 2","#link5",""))
      
    mymenuList.Add(Array As String("2","0","Vinayagar song 1","#link",""))
    mymenuList.Add(Array As String("2","0","Vinayagar song 2","#link",""))
  
    mymenuList.Add(Array As String("3","0","Menu Level 2 >","","4"))
  
    mymenuList.Add(Array As String("4","3","Song","#",""))
 
Top