Android Question CLVTree save content to a file

mike1967

Active Member
Licensed User
Longtime User
Hello is possible to save the content of a CLVTree view to a file in order to retrieve the content on a showed B4xPages ?
The Star-Dust Library SD_Treelist have a method "TreeToJSon".
Is possible to implement this methot also with CLVTree ?
Thanks in Advances
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As CLVTree is a user interface element it can has references to objects that cannot be serialized (bitmaps, CSBuilder and tags). At least not in the general case.

This code will generate a json string from the tree nodes text which can later be used to recreate the tree:
B4X:
Private Sub TreeFromJson (json As String)
    Tree.Clear
    Dim children As List = json.As(JSON).ToList
    TreeFromJsonImpl(Tree.Root, children)
End Sub

Private Sub TreeFromJsonImpl (ParentTreeNode As CLVTreeItem, ParentList As List)
    For Each m As Map In ParentList
        Dim ti As CLVTreeItem = Tree.AddItem(ParentTreeNode, m.Get("text"), Null, "")
        If m.ContainsKey("children") Then
            TreeFromJsonImpl(ti, m.Get("children"))
        End If
    Next
End Sub

Private Sub TreeToJson As String
    Dim l As List
    l.Initialize
    TreeToJsonImpl(Tree.Root, l)
    Return l.As(JSON).ToString
End Sub

Private Sub TreeToJsonImpl (ParentTreeNode As CLVTreeItem, ParentList As List)
    For Each child As CLVTreeItem In ParentTreeNode.Children
        Dim m As Map = CreateMap("text": child.Text)
        ParentList.Add(m)
        If child.Children.IsInitialized And child.Children.Size > 0 Then
            Dim children As List
            children.Initialize
            m.Put("children", children)
            TreeToJsonImpl(child, children)
        End If
    Next
End Sub

It shouldn't be too difficult to extend it for more specific requirements.
 
Upvote 0
Top