我在使用 ExpandableListView 时遇到问题。我想要的是每个组都来自“bond”列,这是非唯一的,但我希望这些组是唯一的(即“bond”的每个值应该只有一个组)。组中的每个值在应显示的其他列中都有数据。问题是,我似乎可以将 SQL DISTINCT 运算符与一列一起使用(然后 getChildrenCursor() 抛出 IllegalStateException),或者不使用 DISTINCT 运算符并复制每个组。
任何人都可以建议解决这个问题吗?
public void onCreate(Bundle saved) {
super.onCreate(saved);
DatabaseHelper dh = new DatabaseHelper(this);
dh.openDataBase();
db = dh.getReadableDatabase();
String query = "SELECT DISTINCT bond FROM spectro";
Cursor c = db.rawQuery(query, null); //throws exception when group opened
//Cursor c = db.query("spectro", new String[] { "_id", "name", "bond", ir },
//null, null, null, null, null) - gives duplicate groups
ExpandableListAdapter a = new IRCursorAdapter (this,
c, android.R.layout.simple_expandable_list_item_1, new String[] { "bond" },
new int[] { android.R.id.text1 }, R.layout.row_doubleend,
new String[] { "name", "ir" }, new int[] { R.id.double_item1, R.id.double_item2 });
this.setListAdapter(a);
}
protected class IRCursorAdapter extends SimpleCursorTreeAdapter {
public IRCursorAdapter(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);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor c = db.query("spectro", new String[] {"_id", "name", "bond", "ir"},
"bond=?", new String[] { groupCursor.getString(0) }, null, null, null);
startManagingCursor(c);
return c;
}
};
这是我的桌子的样子 - 债券是我想要独一无二的:
_id | 姓名 | 债券 | 红外
1 | 名称 1 | 债券 1 | 红外 1
2 | 名称 2 | 债券 1 | 红外 2
3 | 名称 3 | 债券2 | 红外 3
4 | 名称 4 | 债券 3 | 红外线 4
抱歉,如果这不是特别清楚,但感谢您的帮助!