Android - best way to represent and traverse data in a tree structure
I have an object that can be a parent or child of other objects. It is representative of a client. It looks like this
public class Site
{
public int SiteId;
public List<Site> Children;
public Site Parent;
}
When Parent
is null it is a top level or root object. Not all objects have children. We impose no limit on how many levels down the sites can go. Our application is designed as such that Users and other data are tied to a particular site, whether it be a root or a child node anywhere in the tree. For 开发者_如何转开发this reason I have to know the Site that the user is working with to pull back the correct data.
My question is what is the best way to allow a user to traverse this tree? There are a lot of nodes, so returning everything is not going to be desired. Rather than returning all the children in my model object right now, I am instead simply returning a boolean value noting if the site has children. Then when the user clicks on that option, my plan was to go get the children of that node, and show them for selection.
I was thinking about using a ListView
in a similar fashion as most settings menus with an arrow shown if their are options. I also looked at a Menu
but I'm just not sure the best way to do this.
Traversal of a tree basically comes down to depth first or breadth first searches. However from what you've described of your app this may not be the best approach.
One thing I've seen for structures similar to what you described will list the parents (up to the root) and the children of the current viewing node. If you provide more information on how you want to use the traversal of this tree I could try to help you further.
Additionally the above approach only works if their is no loops.
精彩评论