B4A Library [Class] Treeview Class (Tree and Node)

After finding how to communicate between classes I improved the previous version and since it is completely different, I opened a new thread for it.

There is a Tree class, in which you have to define the nature of the tree (what database it displays). This class manages the nodes which are defined by another class.

I implemented a horizontal "scroll panel" to compensate for the scrollview's lack of it.

The main is a demo with one tree in portrait and two trees in landscape node.

Edit: Version 3 has automatic setting of the treeview panel height and width, so linewidth is calculated, not supplied.
The datalist is sorted.
Arrows added to the slidepanel to hint about its use.
Edit: version 3 updated to have automatic width reduction when nodes close.
Version 4 can scroll horizontally thanks to Informatix additions.
Edit: version 5 use Informatix new 2D ScrollView.

Edit: Updated to ver. 6 :
- The tree is isolated from the specific use (like file manager or family tree), the definition of the data that populates the nodes is done at the calling activity using sub GetData which is called by the tree when required.( Edit: not all the way, in the node_open you still have to change the tag and the text of the new nodes to suit other kinds of trees...)
- LongClick on a node is passed for action to the calling activity to a sub NodeLongClicked.
- OpenRoot method enables display of the root node open (by code, the user can of course open it by pressing the + button)
- NullImages - Possibility to remove the images of file and folder (the label will move left accordingly).
- Wait cursor activated smartly (!!) only when opening nodes with many children, not to slow the program too much.

Edit: Updated to ver. 7 :
See details in this post. http://www.b4x.com/forum/additional...ss-treeview-class-tree-node-3.html#post149450

Edit: updated to ver. 8, the only change from 7 is that data type is in the tree class , not in the main.
Also the Reflection library is not used and can be ticked off.

Edit: update to ver 9. I saw that with new devices of high resolution the lineheight is too large so I adjusted it to give better results for all devices.
If you encounter line size problems please tell me (with the specific device parameter).

Edit: see post #85 for a method to open all nodes of the tree by code.

Looking forward to get feedback !

Note: For B4A ver. 7 replace in tree line 190 Doevent with sleep(0)
 

Attachments

  • screen3.png
    screen3.png
    58.8 KB · Views: 3,016
  • screen4.png
    screen4.png
    57.7 KB · Views: 2,634
  • Treeview9.1.zip
    24.6 KB · Views: 1,000
Last edited:

Andy S.

Member
Licensed User
Longtime User
Hi Derez,


I would like to use this class in my app. Is it in the public domain?


Thanks,
Andy
 

deyvitm

Member
Licensed User
Longtime User
someone can pass me an example of a recursive treeview with checkbox in the end node, then do check and uncheck and when you click on a check run an event, something like this:

ZnFTv.gif


thx,
 

derez

Expert
Licensed User
Longtime User
At the example in the first post, in each node there is an imageview and a label, both have the same event, and only the longclick is in use. You can modify the node class to have a checkbox instead and use either the CB event or the label event.
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
regards
Thank you for your work, I want to know how I can update the number of children of a parent after erasing some of them, I made the following code and the integer variable hj is always the same even when the parent item has no children.
tks .

B4X:
Sub Node_Click2(tnode As Node)

Dim hj As Int 
 
hj =  tnode.TN.pnl.NumberOfViews  
' or
hj =  tnode.TN.children.Size 

If hj < 1 Then

tnode.TN.pnl.RemoveView  ' then delete it
' or
tnode.AsView.RemoveView  ' then delete it

move_nodes( tnode.AsView.Top ,-1) ' move lower nodes up

Else
ToastMessageShow("Del first child", False) 
End If 

ToastMessageShow(tnode.TN.level, False)
ToastMessageShow(treeview.Panel.NumberOfViews, False)

End Sub
 

derez

Expert
Licensed User
Longtime User
As it is designed, the tree and node classes are not planned for managing the data, just to display it in the form of a tree.
If you want to delete a node you should add a public sub to tree class, to do that.
You should look at remove_children sub and create a similar one to delete just one child - the node you want to delete, do not remove it until you run remove_children on that node.
The use of RemoveView must be only after taking care of the parameters that hold the data in the tree, like children list of a node etc.
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
thanks for your prompt response , I can only follow the code, I can not create ,so if you have some time and tell me, as remove_children and updating the number of children a node , appreciate you , greetings
 

derez

Expert
Licensed User
Longtime User
PABLO2013:
Like I wrote above, the task is to display a tree of some data (database, files etc). If you want to be able to delete a node from the tree you have to clarify the scenario:
Is it done by the user of the application or by you while creating the application ? If by the user - what event will be used ? What happens to the data that was used to create the tree ? should re-opening the parent node show the deleted node or not ?
I recommend to do the changes in the database and then display the tree to show the new status of the data.
Please explain.
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings, Derez Thanks for your interest , what I want is to do this : is to create a tree structure of items. This structure allows you to add and delete a particular item also change the name of the item , another thing is that each item is perfectly identifiable for specific and general actions,from I 've seen of your great job , it's almost this ....not if i can enter the item at the bottom of each parent and not on top ... (eg: new ... 4-3-2....old ), thanks
https://github.com/bmelnychuk/AndroidTreeView
 

Attachments

  • tree0.PNG
    tree0.PNG
    27.8 KB · Views: 288
Last edited:

derez

Expert
Licensed User
Longtime User
I recommend to create the tree structure in a database and use the treeview class to display it. All the content of the tree should be managed in the database, not in the treeview. Getting the data from the application for the display should be done by
B4X:
Sub GetData( tr As Tree, dir As String)
in the main module,
which has to get the data from the structure you built and pass it to the treeview.
To build the flexibility that you want in the treeview class means writing a completely new class, sorry but I will not do it for you.
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
regards
In this part , how I should declare (tr As Tree) and (Dim da As Data)... the issue is that I get an indication that data as tree are libraries lost ,

Another thing is that is And relpnl.Visible check_rlist ( p_id ( j ) )

Thanks

B4X:
Sub GetData(tr As Tree, st As String) 'ignore
Dim k, ColorSet, P_id(2) As Int 'ignore
Dim str, Tag,Text,BtnTag As String 'ignore
k = st
If treeflag Then                                      ' root
    cur = SQL.ExecQuery2("SELECT Fcod, Mcod FROM FTable WHERE No = ?",Array As String(k))
    cur.Position = 0
    P_id(0) = cur.GetInt("Fcod")
    P_id(1) = cur.GetInt("Mcod")
    For j = 0 To 1
        Dim da As Data
 

derez

Expert
Licensed User
Longtime User
The data is a type that holds all kinds of parameters that you want to pass to the tree from the main.
The relpnl is something which is used in a specific application and is not part of the tree class. You should create your own sub for getting your specific data
 

derez

Expert
Licensed User
Longtime User
I am. State your problem and either me or someone else will help you.
 

PhilE

Member
Licensed User
Longtime User
I am. State your problem and either me or someone else will help you.

"Problem with TreeviewC9 example"

in Tree.bas - at line 70 - If maxright > treeview.Panel.Width Then treeview.Panel.Width = maxright the program stops.

On the Tablet this msg appears:

An error has occurred in sub:Tree_setroot(Line 70)
If maxright > treeview.Panel.Width Then treeview.Panel.Width = maxright
java.lang.NullPointerException:Attempt to read from
field 'int android.view.viewgroup$LayoutParams.width' on a null object reference
 

derez

Expert
Licensed User
Longtime User
I have checked with my tablet and could not make the problem to appear.
Can you give more details ? which tablet you use (and screen size + resolution)
Are you using the example as is or modified ? please attach the code, at least the activity_create code.
Possible cause is that you use SetRoot before adding the treeview to the activity.
 

peacemaker

Expert
Licensed User
Longtime User
The same trouble. But treeview is added to an extra panel already, that was loaded in a layout.
The treeview is ScrollView2D, maybe here the troble root is...
But long ago, in prev B4A versions - there was no such error in my project with the treeview.
 
Last edited:
Top