来自数据库的Android ExpandableListView
我想从本地数据库创建两级ExpandableListView 我希望从数据库类别表中获取名称的组级别来自数据库的Android ExpandableListView,android,sqlite,expandablelistview,Android,Sqlite,Expandablelistview,我想从本地数据库创建两级ExpandableListView 我希望从数据库类别表中获取名称的组级别 category_id | name ------------------- 1 | NameOfCategory 我希望从列表表中获取名称的子级 list_id | name | foreign_category_id -------------------------------- 1 | Listname | 1 我在DatabaseD
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;
}
公共列表getAllCategories()
{
database=dbHelper.getReadableDatabase();
列表类别=新建ArrayList();
Cursor Cursor=database.query(SQLiteHelper.TABLE_CATEGORY,null,null,null,null,null);
cursor.moveToFirst();
而(!cursor.isAfterLast())
{
类别类别=游标位置(游标);
类别。添加(类别);
cursor.moveToNext();
}
cursor.close();
退货类别;
}
现在,将名称添加到组级别很容易,我可以通过以下方式进行操作:
ArrayList<String> groups;
for(Category c : databaseDao.getAllCategories())
{
groups.add(c.getName());
}
ArrayList组;
对于(类别c:databaseDao.getAllCategories())
{
添加(c.getName());
}
现在我想在下面的数组arraylistchildren中将子元素添加到ExpandableListView中代码>
如何让孩子使用正确的组名?
我认为它与groups.indexOf()有关,但在列表表中,我只有一个外来类别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;
}
我将要做一些测试运行,但现在这是可行的,我想不出任何其他解决方案,如果有人有一些意见,这是非常受欢迎的
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++;
}
}