Dynamically create parent and child
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)
- Can this api create list of groups and append each child accordingly?
- 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.
What would I do
- Create java classes to represent your json structure (just properties and relations)
- Parse your json to your java object
- 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.
I can iterate json, but how can I use this library to make a tree. This is my question? The example is not usefull.
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
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?
Every TreeNode contains your specific object to represent, so if you want to find this, you can iterate over them
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.
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());
}
}
}