2012-03-18 91 views
0

我想從本地數據庫創建一個兩級ExpandableListView。Android ExpandableListView來自數據庫

集團層面我想從數據庫類別表

category_id | name 
------------------- 
    1  | NameOfCategory 

的子女的水平我想從列表表得到名稱得到名稱

list_id | name | foreign_category_id 
-------------------------------- 
    1 | Listname |  1 

我有一個方法在DatabaseDAO中獲取表格中的所有值

public List<Category> getAllCategories() 
{ 
    database = dbHelper.getReadableDatabase(); 
    List<Category> categories = new ArrayList<Category>(); 
    Cursor cursor = database.query(SQLiteHelper.TABLE_CATEGORY, null, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while(!cursor.isAfterLast()) 
    { 
     Category category = cursorToCategory(cursor); 
     categories.add(category); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 

    return categories; 
} 

現在將名稱添加到gro達水平是很容易我做了以下方式:

ArrayList<String> groups; 

for(Category c : databaseDao.getAllCategories()) 
      { 
       groups.add(c.getName()); 
      } 

現在我想給孩子添加到ExpandableListView以下數組ArrayList<ArrayList<ArrayList<String>>> children;英寸

如何獲得正確組名下的孩子? 我認爲它必須使用groups.indexOf()進行討論,但在列表中,我只有一個外部category_id而不是實際的名稱。

回答

2

我現在有以下哪些有點作品。

我創建了一個rawQuery來獲取按行分組的列表名稱。

public List<CategoryAndLists> getCategoryAndListNames() 
{ 
    database = dbHelper.getReadableDatabase(); 
    List<CategoryAndLists> calList = new ArrayList<CategoryAndLists>(); 
    Cursor cursor = database.database.rawQuery("SELECT c.category_id, c.name, w.name FROM category AS c, lists AS w WHERE c.category_id = w.category_id ORDER BY c.category_id, w.name", null); 

    cursor.moveToFirst(); 
    while(!cursor.isAfterLast()) 
    { 
     CategoryAndLists categoryAndLists = new CategoryAndLists(); 
     categoryAndLists.setCategory(cursor.getString(0)); 
     categoryAndLists.setWishlist(cursor.getString(1)); 

     System.out.println(cursor.getString(0)); 
     System.out.println(cursor.getString(1)); 

     calList.add(categoryAndLists); 

     cursor.moveToNext(); 
    } 

    return calList; 
} 

,改變了loadData()方法如下:

private void loadData(){ 

     groups= new ArrayList<String>(); 
     children= new ArrayList<ArrayList<ArrayList<String>>>(); 

     int childrenIndex = 0; 
     String lastCategory = ""; 


     for(Category c : databaseDao.getAllCategories()) 
     { 
      if(!groups.contains(c.getName())) 
      { 
       groups.add(c.getName()); 
      } 
     } 

     for(CategoryAndLists c : databaseDao.getCategoryAndListNames()) 
     { 
      if(!children.contains(c.getWishlist())) 
      { 

        if(lastCategory.equals(c.getCategory())) 
        { 
         childrenIndex++; 
         System.out.println("childrenIndex++ Called"); 
        } 
        else 
        { 
         childrenIndex = 0; 
         System.out.println("childrenIndex = 0 Called"); 
        } 

       //Get the groups ArrayList index id to add the children to 
       int index = groups.indexOf(c.getCategory()); 

       System.out.println("INDEX CHILDRENINDEX: " +childrenIndex); 

       children.add(new ArrayList<ArrayList<String>>()); 
       children.get(index).add(new ArrayList<String>()); 
       children.get(index).get(childrenIndex).add(c.getWishlist()); 

       childrenIndex++; 
      } 

     } 

我打算做幾個測試運行的,但現在這工作,我想不出任何其他的解決方案,如果有人有一些輸入,這是非常值得歡迎的。