B4J Question TreeView Search

Solution
The following code creates a random tree and then removes a tree item based on the text.

Create a layout file with a TreeView named TreeView1.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TreeView1 As TreeView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   FillTree
   Dim res As TreeItem = Search(TreeView1.Root, "23")
   If res.IsInitialized Then
     Dim i As Int = res.Parent.Children.IndexOf(res)
     res.Parent.Children.RemoveAt(i)
   End If
End Sub

'Returns an uninitialized TreeItem if not found.
Sub Search(Parent As TreeItem, s As String) As TreeItem
   For Each ti As TreeItem In Parent.Children
     If...

derez

Expert
Licensed User
Longtime User
I do it by keeping the data in a database (sqlite), doing the search in the database and using the treeview just for display of the results.
If you do not have the data in a database (for example, to search for a file - the list of files in every node is found directly) - then you have to work recursively - check the root node for each of its children, then check each of the children for its own children etc. Seems like re-inventing the wheel...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code creates a random tree and then removes a tree item based on the text.

Create a layout file with a TreeView named TreeView1.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TreeView1 As TreeView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   FillTree
   Dim res As TreeItem = Search(TreeView1.Root, "23")
   If res.IsInitialized Then
     Dim i As Int = res.Parent.Children.IndexOf(res)
     res.Parent.Children.RemoveAt(i)
   End If
End Sub

'Returns an uninitialized TreeItem if not found.
Sub Search(Parent As TreeItem, s As String) As TreeItem
   For Each ti As TreeItem In Parent.Children
     If ti.Text = s Then
       Return ti
     End If
     If ti.Children.Size > 0 Then
       Dim res As TreeItem = Search(ti, s)
       If res.IsInitialized Then Return res
     End If
   Next
   Dim res As TreeItem
   Return res
End Sub

Sub FillTree
   Dim n As TreeItem = TreeView1.Root
   For i = 1 To 100
     Dim ti As TreeItem = CreateNode(i)
     n.Children.Add(ti)
     n.Expanded = True
     Dim r As Int = Rnd(0, 4)
     If r = 1 Then
        n = ti
     Else if r = 0 And n.Root = False Then
       n = n.Parent
     End If
   Next
End Sub

Sub CreateNode(s As String) As TreeItem
   Dim ti As TreeItem
   ti.Initialize("", s)
   Return ti
End Sub
 
Upvote 1
Solution

Mashiane

Expert
Licensed User
Longtime User
The following code creates a random tree and then removes a tree item based on the text.

Create a layout file with a TreeView named TreeView1.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TreeView1 As TreeView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   FillTree
   Dim res As TreeItem = Search(TreeView1.Root, "23")
   If res.IsInitialized Then
     Dim i As Int = res.Parent.Children.IndexOf(res)
     res.Parent.Children.RemoveAt(i)
   End If
End Sub

'Returns an uninitialized TreeItem if not found.
Sub Search(Parent As TreeItem, s As String) As TreeItem
   For Each ti As TreeItem In Parent.Children
     If ti.Text = s Then
       Return ti
     End If
     If ti.Children.Size > 0 Then
       Dim res As TreeItem = Search(ti, s)
       If res.IsInitialized Then Return res
     End If
   Next
   Dim res As TreeItem
   Return res
End Sub

Sub FillTree
   Dim n As TreeItem = TreeView1.Root
   For i = 1 To 100
     Dim ti As TreeItem = CreateNode(i)
     n.Children.Add(ti)
     n.Expanded = True
     Dim r As Int = Rnd(0, 4)
     If r = 1 Then
        n = ti
     Else if r = 0 And n.Root = False Then
       n = n.Parent
     End If
   Next
End Sub

Sub CreateNode(s As String) As TreeItem
   Dim ti As TreeItem
   ti.Initialize("", s)
   Return ti
End Sub
Thanks a lot Erel, this will do. Perfect!!!
 
Upvote 0
Top