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

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:



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
    27.8 KB · Views: 233
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:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…