2017-01-30 18 views
1

リストビューでログインをクリックすると、このツリーメニューのリストビューを作成したいと思っています。 (フラグメントのような) このような断片を使ってこれを行うには、より良い方法がありますか? 私は最初のフラグメントメニューをしたいがログイン、レポート、ログインメニュープレスで管理別のフラグメントの負荷を持っており、それはAndroidのツリーデータ構造のリストビューを作成

ユーザーIDとユーザー・ピン

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); 
     } 
    } 
} 

答えて

0

まず、あなたのノードクラスに「int UserRole」を追加する必要があると思います。

次に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); 
    } 

     } 
+0

なぜすべての単語に大文字で書いていますか? – Mistalis

+0

はい、私はすでにこれを作成し、どのようにXMLの断片と接続しますか? –

+0

UpperCaseについて私は残念です – Elsunhoty

関連する問題