问题
I want to create a two level ExpandableListView from a local database.
The group level I want to get the names from the database Category table
category_id | name
-------------------
1 | NameOfCategory
the children level I want to get the names from the List table
list_id | name | foreign_category_id
--------------------------------
1 | Listname | 1
I've got a method in DatabaseDAO to get all the values from the table
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;
}
Now adding the names to the group level is easy I do it the following way:
ArrayList<String> groups;
for(Category c : databaseDao.getAllCategories())
{
groups.add(c.getName());
}
Now I want to add the children to the ExpandableListView in the following array ArrayList<ArrayList<ArrayList<String>>> children;
.
How do I get the children under the correct group name? I think it has to do somenthing with groups.indexOf() but in the list table I only have a foreign category_id and not the actual name.
回答1:
I've got the following which kinda works now.
I've created a rawQuery to get get the list names grouped with the category in rows.
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;
}
And changed the loadData()
method to the following:
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++;
}
}
I'm going to do a couple of test runs but for now this works and I can't think of any other solution, if someone has some input, it's more than welcome.
来源:https://stackoverflow.com/questions/9758645/android-expandablelistview-from-database