在Android中为树数据结构创建列表视图

时间:2017-01-30 10:43:07

标签: java android

如果我在列表视图中点击登录进入用户ID,我想为此树状菜单创建列表视图。 (比如使用片段) 使用像这样的片段有更好的方法吗? 我希望第一个片段菜单在登录菜单中有登录,报告,管理员按下另一个片段加载,它有用户ID和用户PIN

MainActivity.java

public class MainActivity extends AppCompatActivity { 

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


        }

        public static void main(String[] args) {

            Node treeRootNode = new Node(null);
            treeRootNode.setId("Main Menu");
            // add child to root node
            Node child1= addChild(treeRootNode, "Login");
            // add child to the child node created above
            addChild(child1, "User ID");
            addChild(child1, "User PIN");


            // add child to root node
            Node child2 = addChild(treeRootNode, "Report");
            // add child to the child node created above
            addChild(child2, "Shift Report");
            addChild(child2,"Period Report");

            Node child3 = addChild(treeRootNode, "Admin");

            addChild(child3,"change PiN");
            addChild(child3,"Change Role");


            printTree(treeRootNode, " ");

        }

        private static Node addChild(Node parent, String id) {
            Node node = new Node(parent);
            node.setId(id);
            parent.getChildren().add(node);
            return node;
        }

        private static void printTree(Node node, String appender) {

            System.out.println(appender + node.getId());

            for (Node each : node.getChildren()) {
                printTree(each, appender + appender);
            }
        }
}

Node.java

    public class Node {

    private String id;
    private final List<Node> children = new ArrayList<>();
    private final Node parent;

    public Node(Node parent) {
        this.parent = parent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Node> getChildren() {
        return children;
    }

    public Node getParent() {
        return parent;
    }


    private static Node addChild(Node parent, String id) {
        Node node = new Node(parent);
        node.setId(id);
        parent.getChildren().add(node);
        return node;
    }

    private static void printTree(Node node, String appender) {

        System.out.println(appender + node.getId());

        for (Node each : node.getChildren()) {
            printTree(each, appender + appender);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先我想你需要添加&#34; int UserRole&#34;在您的节点类

然后编辑ListView适配器

 public View getView(final int position, View convertView, ViewGroup parent)       {

    View view = convertView;
    Int Role= NodeModel.get(position).getRole;

    if (Role ==1) 
        {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        view = layoutInflater.inflate(layoutResource, null);
     }

      if (Role ==2) 
        {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        view = layoutInflater.inflate(layoutResource, null);
     }

        }