2013-12-10 2 views
2

Я хочу реализовать структуру дерева N-уровня в android. Я прошел через ExpandableListView, но он применим только для 2 или 3 уровней. Мои требования ниже ...Структура дерева N-уровня в реализации android

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); 
     } 
Смежные вопросы