【问题标题】:Android ExpandableListView From Database来自数据库的 Android ExpandableListView
【发布时间】:2012-04-03 06:11:37
【问题描述】:

我想从本地数据库创建一个二级 ExpandableListView。

我想从数据库Category表中获取名称的组级

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

现在我想将子元素添加到以下数组 ArrayList&lt;ArrayList&lt;ArrayList&lt;String&gt;&gt;&gt; children; 中的 ExpandableListView。

如何让孩子使用正确的组名? 我认为它必须与 groups.indexOf() 做一些事情,但在列表中我只有一个外国 category_id 而不是实际名称。

【问题讨论】:

    标签: android sqlite expandablelistview


    【解决方案1】:

    我有以下现在可以使用的。

    我创建了一个 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++;
                }
    
            }
    

    我将进行几次测试运行,但目前这可行,我想不出任何其他解决方案,如果有人有意见,我们非常欢迎。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-25
      • 2019-05-03
      • 2012-08-30
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多