B4J Question Moving TreeItem up the hierarchy strips the alignment

TorontoJim

Member
Licensed User
Longtime User
With the TreeView, I have it adding children no problem. I also have it able to take a child and make it a sibling of the parent (move up a level in the hierarchy of the family).

However, when I do this, it buggers up how that new sibling is displayed. It displays it all the way to the left instead of in the proper position. I know it is a sibling and not a parent because it collapses when the parent closes.

This is the code to make a child a parent (sibling of the parent), but I haven't found anything in the forum that addresses this issue.

B4X:
Public Sub MakeParent(objNodeSelected As TreeItem)
	If objNodeSelected.IsInitialized = True Then
		Dim parentNode As TreeItem = objNodeSelected.Parent
		If parentNode.IsInitialized = True Then
			Dim grandparentNode As TreeItem = parentNode.Parent
			If grandparentNode.IsInitialized = True Then
				Dim intIndexOfParent As Int = grandparentNode.Children.IndexOf(parentNode)
				Dim intOriginalTreeItemIndex As Int = parentNode.Children.IndexOf(objNodeSelected)
				If intIndexOfParent = grandparentNode.Children.Size - 1 Then
					grandparentNode.Children.Add(objNodeSelected)
				Else
					grandparentNode.Children.InsertAt(grandparentNode.Children.IndexOf(parentNode) + 1, objNodeSelected)
				End If
				parentNode.Children.RemoveAt(intOriginalTreeItemIndex)
			End If
		End If
	End If
End Sub

Below are the screen shots showing what I'm talking about. So how do I set the property on a child that has been moved up the hierarchy, to ensure its alignment?

Screenshot 2016-04-13 17.25.21.png


Screenshot 2016-04-13 17.25.30.png


Screenshot 2016-04-13 17.25.40.png


Screenshot 2016-04-13 17.25.45.png
 

TorontoJim

Member
Licensed User
Longtime User
Just a bit of clarification, it doesn't matter whether grandparentNode.Children.Add or grandparentNode.Children.InsertAt is used, the result is the same.
 
Upvote 0

TorontoJim

Member
Licensed User
Longtime User
Works perfectly. Thank you.

I needed three arguments for one of the logical conditions but CallSubDelayed3 only allows two arguments. Since two of the arguments were the same type, I put them in an array and passed the array as one of the arguments.

Here is the modified code for posterity:

B4X:
Public Sub MakeParent(objNodeSelected As TreeItem)
	If objNodeSelected.IsInitialized = True Then
		Dim parentNode As TreeItem = objNodeSelected.Parent
		If parentNode.IsInitialized = True Then
			Dim grandparentNode As TreeItem = parentNode.Parent
			If grandparentNode.IsInitialized = True Then
				Dim intIndexOfParent As Int = grandparentNode.Children.IndexOf(parentNode)
				Dim intOriginalTreeItemIndex As Int = parentNode.Children.IndexOf(objNodeSelected)
				If intIndexOfParent = grandparentNode.Children.Size - 1 Then
					Dim tempNode As TreeItem
					tempNode = objNodeSelected
					parentNode.Children.RemoveAt(intOriginalTreeItemIndex)
					CallSubDelayed3(Me, "MakeParent_Add", grandparentNode, tempNode)
				Else
					Dim tempNode As TreeItem
					tempNode = objNodeSelected
					Dim args(3) As TreeItem
					args(0) = grandparentNode
					args(1) = tempNode
					Dim intNewIndex As Int = (grandparentNode.Children.IndexOf(parentNode) + 1)
					parentNode.Children.RemoveAt(intOriginalTreeItemIndex)
					CallSubDelayed3(Me, "MakeParent_Insert", args, intNewIndex)
				End If
			End If
		End If
	End If
End Sub

Private Sub MakeParent_Add(grandParentNode As TreeItem, newNode As TreeItem)
	grandParentNode.Children.Add(newNode)
End Sub

Private Sub MakeParent_Insert(args() As TreeItem, atIndex As Int)
	Dim grandparentNode As TreeItem = args(0)
	If grandparentNode.IsInitialized = True Then
		Dim newNode As TreeItem = args(1)
		If newNode.IsInitialized = True Then
			grandparentNode.Children.InsertAt(atIndex, newNode)
		End If
	End If
End Sub
 
Upvote 0
Top