我正在为Android开发一个应用程序,我正在使用ExpandableListActivity。
我正在从以下格式的 sqlite db 中提取数据:
Id | Category | Name
这里的CODE
是抓取数据的代码:
List<String> groups = new ArrayList<String>();
List<String> books = new ArrayList<String>();
List<String> elecs = new ArrayList<String>();
List<List<String>> children = new ArrayList<List<String>>();
Cursor c = mDbHelper.fetchAllItems();
startManagingCursor(c);
// for all rows
for(int i=0; i<c.getCount(); i++)
{
c.moveToNext();
String t = c.getString(c.getColumnIndex("category"));
switch (getCat(t))
{
case Books:
books.add(c.getString(c.getColumnIndex("name")));
Log.d("FROM: ", c.getString(c.getColumnIndex("name")));
if(!(groups.contains("Books"))) groups.add("Books");
break;
case Electronics:
elecs.add(c.getString(c.getColumnIndex("name")));
Log.d("FROM: ", c.getString(c.getColumnIndex("name")));
if(!(groups.contains("Electronics"))) groups.add("Electronics");
break;
default:
break;
} // end switch
} // end for loop
children.add(books);
children.add(elecs);
当我运行这个应用程序时的输出
是屏幕的样子:
Books
Electronics
当我点击书籍或电子产品时,它会显示所有内容。像这样:
Books
[c programming, perl]
[cd, laptop, psp]
Electronics
[c programming, perl]
[cd, laptop, psp]
ISSUE
我认为问题来自这种方法(getChild):
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
List<String> GRP;
List<List<String>> CHLD;
public MyExpandableListAdapter(List<String> grps, List<List<String>> chldrn) {
GRP = grps;
CHLD = chldrn;
}
...
...
public Object getChild(int groupPosition, int childPosition) {
// CHLD[groupPosition][childPosition]; ORIGINAL, where CHLD was a 2d array
return CHLD.get(childPosition); MODIFIED
}
我必须将我的字符串列表转换为二维数组吗?