簡體   English   中英

來自數據庫的Android ExpandableListView

[英]Android ExpandableListView From Database

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

組級別我想從數據庫類別表中獲取名稱

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

子級別我想從List表中獲取名稱

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

現在將名稱添加到組級別很容易我通過以下方式執行:

ArrayList<String> groups;

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

現在我想將子節點添加到以下數組中的ExpandableListView: ArrayList<ArrayList<ArrayList<String>>> children;

如何讓孩子接受正確的團體名稱? 我認為必須使用groups.indexOf()進行somenthing,但在列表中我只有一個外來的category_id而不是實際的名字。

我現在有以下哪種方式有效。

我已經創建了一個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++;
            }

        }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM