I'm looking for something to crate TreeView in my application but i'm unable to create Node object using loop.
i just want to create dynamically object name which i can use in my TreeView.
Here is the library which i used in my application. TreeView
for(int i = 0; i< arrId.size(); i++){
final Node i = new Node(getNodeText(i));
}
Also i try to do this one and i'm getting error is "node" variable is already declared in scope.
for(int loopI = 0; loopI< arrId.size(); loopI++){
String node = "node"+arrId.get(loopI);
final Node node = new Node(getNodeText(loopI));
}
Here Is the Static Code that working good.
final Graph graph = new Graph();
final Node node1 = new Node(getNodeText());
final Node node2 = new Node(getNodeText());
final Node node3 = new Node(getNodeText());
final Node node4 = new Node(getNodeText());
final Node node5 = new Node(getNodeText());
final Node node6 = new Node(getNodeText());
final Node node8 = new Node(getNodeText());
final Node node7 = new Node(getNodeText());
final Node node9 = new Node(getNodeText());
final Node node10 = new Node(getNodeText());
final Node node11 = new Node(getNodeText());
final Node node12 = new Node(getNodeText());
graph.addEdge(node1, node2);
graph.addEdge(node1, node3);
graph.addEdge(node1, node4);
graph.addEdge(node2, node5);
graph.addEdge(node2, node6);
graph.addEdge(node6, node7);
graph.addEdge(node6, node8);
graph.addEdge(node4, node9);
graph.addEdge(node4, node10);
graph.addEdge(node4, node11);
graph.addEdge(node11, node12);
Maybe you should override method getNodeText()
because you want to pass the argument. Or just dont pass the argument like getNodeText(i)
because this method already have counter.
private String getNodeText() {
return "Node " + nodeCount++;
}
My feeling is that you should be using some sort of collection here. Consider the following:
final List<Node> allNodes = new ArrayList<>();
for (int i=0; i < arrId.size(); i++) {
Node node = new Node(getNodeText(i));
allNodes.add(node);
}
Then, when you want to build your tree view:
final Graph graph = new Graph();
graph.addEdge(node.get(0), node.get(1));
graph.addEdge(node.get(0), node.get(2));
graph.addEdge(node.get(0), node.get(3));
graph.addEdge(node.get(1), node.get(4));
graph.addEdge(node.get(1), node.get(5));
graph.addEdge(node.get(5), node.get(6));
graph.addEdge(node.get(5), node.get(7));
graph.addEdge(node.get(3), node.get(8));
graph.addEdge(node.get(3), node.get(9));
graph.addEdge(node.get(3), node.get(10));
graph.addEdge(node.get(10), node.get(11));
The idea here is that you should not be trying to number your variables yourself. Most of the time, when you have this need, you should instead be using a collection. Most list implementations have a built in counter which can be used to number the elements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.