我在使用rawQuery获取游标以在ExpandableListView中使用时遇到了一些麻烦。我有一个名为ExpandableDataAdapter的适配器类,它扩展了SimpleCursorTreeAdapter。以下是我必须创建ExpandableDataAdapter实例的行:
ExpandableDataAdapter confDataAdapter = new ExpandableDataAdapter(
this,
groupCursor,
R.layout.detaillist_group,
new String[]{ "TrackName" },
new int[]{ R.id.rowText },
R.layout.detaillist_child,
new String[]{ "Title", "Speakers", "TrackName", "Room" },
new int[]{ R.id.sessionTitle, R.id.sessionSpeakers, R.id.sessionTrack, R.id.sessionRoom }
);
在ExpandableDataAdapter类定义中,我有以下方法来获取组游标的子游标:
protected Cursor getChildrenCursor(Cursor groupCursor)
{
// Given a group, return a cursor for all the children in that group.
Cursor talksCursor = _db.rawQuery("SELECT t1.TalkID as _id, t1.Title," +
" 'None' as Speakers, t2.TrackName, t1.Room" +
" from talks t1, tracks t2" +
" where t1.TrackID = t2.TrackID" +
" and t2.TrackID=?", new String[]{ groupCursor.getString(groupCursor.getColumnIndex("_id")) });
startManagingCursor(talksCursor);
return talksCursor;
}
以上查询有效,并显示ExpandableListView。但是,当然,上面是我想要执行的实际查询的更简单版本。当我用实际(并且稍微复杂一点)的查询替换它时,我得到一个例外。首先,查询:
protected Cursor getChildrenCursor(Cursor groupCursor)
{
// Given a group, return a cursor for all the children in that group.
Cursor talksCursor = _db.rawQuery("SELECT t1.TalkID as _id, t1.Title," +
" group_concat(t3.Speaker, ', ') as Speakers, t2.TrackName, t1.Room" +
" from talks t1, tracks t2, talkspeaker t3" +
" where t1.TalkID = t3.TalkID" +
" and t1.TrackID = t2.TrackID" +
" and t2.TrackID=?" +
" group by _id, t1.Title, t2.TrackName, t1.Room",
new String[]{ groupCursor.getString(groupCursor.getColumnIndex("_id")) });
startManagingCursor(talksCursor);
return talksCursor;
}
我得到的例外是:列'房间'不存在。这个最新的查询在数据库提示符下运行正常,所以我猜测在android引擎执行时会发生一些事情。此外,异常声明也让我感到难过。我用了太多桌子吗? group_concat函数是否会造成麻烦? 有什么指针吗?
更多注意事项:从适配器的构造函数抛出异常,其定义如下。我正在调用构造函数,如本文的第一个片段所示。
public ExpandableDataAdapter(Context context, Cursor cursor,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childFrom, int[] childTo)
{
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childFrom, childTo);
}
答案 0 :(得分:1)
这是你的SQL的一个问题。您只能选择Group By子句中的字段。 SELECT子句中的所有其他字段必须是聚合。因此,如果您的数据集可以实现,则可能必须将t2.TrackName
和t1.Room
添加到Group By子句中。
答案 1 :(得分:0)
想出来 - 看起来我必须在第二个查询中显式声明列名。坦率地说,我不确定这是否确实是问题,或者这是否是正确的解决方案,但在列表中,ExpandableListView正在显示。工作片段是:
Cursor talksCursor = _db.rawQuery("SELECT t1.TalkID as _id, t1.Title as Title," +
" group_concat(t3.Speaker, ', ') as Speakers, t2.TrackName as TrackName, t1.Room as Room" +
" from talks t1, tracks t2, talkspeaker t3" +
" where t1.TalkID = t3.TalkID" +
" and t1.TrackID = t2.TrackID" +
" and t2.TrackID=?" +
" group by _id, t1.Title, t2.TrackName, t1.Room",
new String[]{ groupCursor.getString(groupCursor.getColumnIndex("_id")) });