Clear a node's children
Hi,
My main tree got couple of main nodes, and a node which contains the last used leafs. so whenever the fragment containing the treeView reloads I need to clear the last used node and repopulate it with my logic.
the problem is when I do a foreach loop on treenode.getChildren() and call treeNode.deleteChild(tNode); I get java.util.ConcurrentModificationException
any idea on how to do this ?
I also tried to remove the treeNode itself and re add it, but it is a bit buggy, because the re added node, although is not expanded in the UI, but for the first time, it needs two touches to get expanded.
well that problem resolved by copying the getChildren list and do the loop on the copy,
after changing the node's children, it seems that the tree does not sense the modification, and does not show the new list. is there any way to accomplish this? something like adapter.notifyDataSetChanged() or so ?
apparently collapsing and then expanding the node does the trick, but I'm sure it's gotta be a cheaper way to do this as well, or at least it is better to be...
I think you are right I should add something like notifyDataSetChanged(), unfortunately after view is rendered it's not going to be updated after model is updated. I will add this in next version Thanks
Hi,
I was wondering if there is any update on this matter.
I'm currently trying to update a mid-level TreeNode by deleting it, and adding a new updated object. I save the children in a temp List<> and add them to the new object and re-toggle the parent node.
However, I get a The specified child already has a parent. You must call removeView() on the child's parent first. exception when I click the new object.
private void updateTreeItem(TreeNode item, Category newValues){ TreeNode parentItem = item.getParent(); List<TreeNode> childes = item.getChildren(); mTreeView.toggleNode(parentItem); parentItem.deleteChild(item); item = new TreeNode(newValues).setViewHolder(new TreeViewItem(getContext())); item.addChildren(childes); parentItem.addChild(item); mTreeView.toggleNode(parentItem); }
Any idea on how to solve this?... at least till the next update
I think you have to delete the list from the end to the beginning like
for (int i = mNode.getChildren().size(); i >= 0; i--) { mNode.getChildren().remove(i); }
This is because of the structure of a list. Each item contains its next item, and if you remove it, you can't access its next anymore, so you have to delete it from the tail to the head.
Any news about clear all children?)