bmelnychuk / AndroidTreeView

AndroidTreeView. TreeView implementation for android
Apache License 2.0
3k stars 620 forks source link

who the treeview's parent #129

Open wangxiaowei000 opened 7 years ago

wangxiaowei000 commented 7 years ago

The specified child already has a parent. You must call removeView() on the child's parent first. I want to remove the existing data and Add new data,but has a Error

renyuneyun commented 7 years ago

Probably I have a different problem, but the bahavior is the same:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I'm trying to create a context menu for the tree view, so I used registerForContextMenu(tView.getView()), but when the app runs this error is thrown.

The code looks like this:

// Created the tree (root node)
LinearLayout layout = new LinearLayout(getActivity());
AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
layout.addView(tView.getView());
registerForContextMenu(tView.getView());

So, how can I bind context menu to the AndroidTreeView? Or, am I only able to use tView.setDefaultNodeLongClickListener() and create a dialog myself?

Thanks in advance :)

DipaliShah commented 7 years ago

Hello Folks,

I find the same issue while adding AndroidTreeViewto RelativeLayout. Is there any Solution ?

Any Help would be appreciated.

vssh commented 7 years ago

I had the same problem. What I was doing wrong was I was setting the same viewHolder on each node instead of instantiating a new one for each. This meant it was trying to attach the same view to all the nodes. So basically, don't do this:

MyViewHolder myViewHolder = new MyViewHolder();
for(TreeNode node : nodes) {
    node.setViewHolder(myViewHolder);
}

Do this:

for(TreeNode node : nodes) {
    MyViewHolder myViewHolder = new MyViewHolder();
    node.setViewHolder(myViewHolder);
}

Hopefully, this helps someone having a similar problem.