2013-12-10 96 views
2

我想在android中實現N級樹結構。我已經通過了ExpandableListView,但它僅適用於2或3個級別。我的要求如下...Android實現中的N級樹結構

CAR 
| 
|==Toyota 
|  | 
|  |==Sedan 
|  |  | 
|  |  |==Camry 
|  |  | | 
|  |  | |==Manufacture year 
|  |  | |==Body Colour 
|  |  | | | 
|  |  | | |==Black 
|  |  | | |==Blue 
|  |  | | |==Red 
|  |  | |  
|  |  | |==Alloy Wheels 
|  |  | |==CD player 
|  |  | |==Petrol/Diesel 
|  |  |  
|  |  |==Yaris 
|  |   | 
|  |   |==Manufacture year 
|  |   |==Body Colour 
|  |   | | 
|  |   | |==Black 
|  |   | |==Blue 
|  |   | |==Red 
|  |   |  
|  |   |==Alloy Wheels 
|  |   |==CD player 
|  |   |==Petrol/Diesel 
|  | 
|  |==Hatch Pack 
|  |  | 
|  |  |==Yaris 
|  |   | 
|  |   |==Manufacture year 
|  |   |==Body Colour 
|  |   | | 
|  |   | |==Black 
|  |   | |==Blue 
|  |   | |==Red 
|  |   |  
|  |   |==Alloy Wheels 
|  |   |==CD player 
|  |   |==Petrol/Diesel 
|  | 
|  | 
|  | 
|  |==Four Wheel Drive 
| 
| 
|==Mazda 
| 
| This section will have 
| same classification as above 
| 
|==Nissan 
| 
| This section will have 
| same classification as above 
| 
|==Ferrari 
| 
| This section will have 
| same classification as above 
| 
|==Hyundai 
| 
| This section will have 
| same classification as above 
| 

你有沒有什麼建議可以在Android中實現。示例代碼會更有幫助。提前致謝。

回答

0

你基本上可以更新某些元素並改變它們的標題可見性以顯示android中的樹結構。 使用下面的java代碼來存儲你的樹值。它也適用於android。檢查this答案details.Make一個Java類

package com.example.drawmanager; 

import java.util.*; 
//source code for n-ary tree traversal 
//https://stackoverflow.com/questions/12788641/level-order-traversal-of-a-generic-treen- ary-tree-in-java 
class NaryTree { 
final String data; 
final List<NaryTree> children; 

// constructor 
public NaryTree(String data, NaryTree... children) { 
    this.data = data; 
    this.children = Arrays.asList(children); 
} 
//class to iterate over the set of values rowwize 
static class InOrderIterator implements Iterator<String> { 
    //queue to handle elements of tree 
    final Queue<NaryTree> queue = new LinkedList<NaryTree>(); 

    //constructor to add elements 
    public InOrderIterator(NaryTree tree) { 
     queue.add(tree); 
    } 

    //boolean check for next element in queue 
    @Override 
    public boolean hasNext() { 
     return !queue.isEmpty(); 
    } 

    //get the next element 
    @Override 
    public String next() { 
     NaryTree node = queue.remove(); 
     queue.addAll(node.children); 
     return 
       node.data; 
    } 

    //check for exceptions 
    @Override 
    public void remove() { 
     throw new UnsupportedOperationException(); 
    } 
} 
// traverse the tree 
Iterable<String> inOrderView = new Iterable<String>() { 
    @Override 
    public Iterator<String> iterator() { 
     return new InOrderIterator(NaryTree.this); 
    } 
}; 
} 

測試代碼:此代碼添加到您的主要活動作爲按鈕的的onclick的一部分,並檢查輸出記錄。我沒有添加你的圖表的完整列表,但我希望你明白了。

// code for tranversing and storing the tree elements 
    //one tree and its elements 
    NaryTree tree = new NaryTree("Car", 
      new NaryTree("Toyota",   
       new NaryTree("Sedan"), 
       new NaryTree("HatchBack"), 
       new NaryTree("FourWheel Drive") 
      ) , 
       new NaryTree("Mazda"), 
       new NaryTree("Nissan"), 
       new NaryTree("Ferrai"), 
       new NaryTree("Hundai") 
     ); 
     for (String x : tree.inOrderView) { 
      // System.out.println(x); 
      //traverse the tree row wise 
      Log.d("tree elements"," "+x); 
     }