AndroidTreeView icon indicating copy to clipboard operation
AndroidTreeView copied to clipboard

Dynamically create parent and child

Open mohammedfaisalshaikh opened this issue 9 years ago • 7 comments

I have a 2 json:

  • first is group name (like a folder structure), each group can have multiple groups.
  • second is list of users, a user can be child of any group (like a file in folder)
  1. Can this api create list of groups and append each child accordingly?
  2. Can I get onclick action on each user.

I have implemented in web like this

Can I implemented the same using this API in Android. If yes then please send me good example.

mohammedfaisalshaikh avatar May 19 '16 07:05 mohammedfaisalshaikh

What would I do

  1. Create java classes to represent your json structure (just properties and relations)
  2. Parse your json to your java object
  3. Iterate over your objects and create tree out of it

Its possible to represent any tree view structure with library, but parsing json and iterating over relations is not a related to this library.

bmelnychuk avatar May 19 '16 09:05 bmelnychuk

I can iterate json, but how can I use this library to make a tree. This is my question? The example is not usefull.

mohammedfaisalshaikh avatar May 19 '16 09:05 mohammedfaisalshaikh

Example covers everything.

YourCompilcatedTree sctructure = parseFromJson();
TreeNode root = TreeNode.root();

TreeNode usersRoor = new TreeNode();
for(User u : sctructure.getUsers()) {
   TreeNode uNode = new TreeNode();
   usersRoor.addChildren(uNode);
}
root.addChildren(usersRoor);

This is just pseudo code, but its nothing more than iterting over objects and create TreeNodes

bmelnychuk avatar May 19 '16 10:05 bmelnychuk

TreeNode uNode = new TreeNode();

It seems above code will create node dynamically.

But the problem is after creating all the nodes how can I add child to particular node. Is there any way to add node with additional information, and using this information append new child?

mohammedfaisalshaikh avatar May 19 '16 11:05 mohammedfaisalshaikh

Every TreeNode contains your specific object to represent, so if you want to find this, you can iterate over them

bmelnychuk avatar May 19 '16 11:05 bmelnychuk

Let me clear my doubt:

Suppose I will create node dynamically like:

TreeNode usersRoor = new TreeNode();
for(User u : sctructure.getUsers()) {
   usersRoor.addChildren(new TreeNode());
}

This look will create nodes dynamically for me, but now what should I do If I want to add child in dynamically created node.

mohammedfaisalshaikh avatar May 19 '16 12:05 mohammedfaisalshaikh

Hi, I had a similar issue. I wanted to represent a dynamically created menu. I'll post you a sample code.

First of all I created a Java class that represented my structure; my object was something like this:

public class MyObject {
    private String name;
    private LinkedList<MyObject> listOfChildren;
}

public LinkedList<MyObject> getListOfChildren(){
    ...
}

//override toString in order to print your object value in the tree view
public String toString(){
    return this.getName();
}

Then I created a recursive method to read and put my data in the TreeNode:

private void addChildren(TreeNode parent, LinkedList<MyObject> myOList) {
    for (MyObject myO : myOList) {
        TreeNode tn1 = new TreeNode(myO);
        parent.addChild(tn1);
        if (myO.getListOfChildren().size() > 0) {
            addChildren(tn1, myO.getListOfChildren());
        }
    }
}

adryx92 avatar Jul 25 '16 09:07 adryx92