我正在尝试使用SimpleCursorTreeAdapter构建expandableList,但我的列表不会创建组,它会将每个子元素重复为组元素。
我的数据形成为KEY_ROWID,KEY_NAME,KEY_EMAIL,KEY_ILID,KEY_KURUMID,KEY_BOLGEID
我的数据:
(db,"Asli", "asli@gmail.com", 1, 1, 1);
(db,"Osman Can", "osman@gmail.com", 1, 1, 1);
(db,"Abuzer Kadayif", "kadayif@gmail.com", 1, 2, 1);
(db,"Emre", "emre@gmail.com", 1, 3, 2);
(db,"Deniz", "deniz@gmail.com", 1, 4, 2);
(db,"Simon Garfunkel", "simon@gmail.com", 1, 5, 0);
(db,"Eric Clapton", "eric@gmail.com", 1, 6, 0);
(db,"Neil Young", "neil@gmail.com", 1, 5, 0);
这是我的代码:
String sqlString = "SELECT * " + " FROM " + Items.DATABASE_TABLE;
Cursor mCursor = db.rawQuery(sqlString, null);
SimpleCursorTreeAdapter mAdapter = new SimpleCursorTreeAdapter(this,
mCursor, R.layout.row, R.layout.exprow,
//createGroupList(),
new String[] { Items.KEY_BOLGEID },
new int[] { R.id.txtItem },
R.layout.exprow, R.layout.exprow,
new String[] { Items.KEY_EMAIL,Items.KEY_NAME }, new int[] { R.id.dscItem, R.id.manuItem }) {
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
String tempGroup = groupCursor.getString(groupCursor
.getColumnIndex(Items.KEY_BOLGEID));
DbHelper dbh = new DbHelper(BrowseActivity.this);
SQLiteDatabase db = dbh.getWritableDatabase();
String sqlString = "SELECT " + Items.KEY_ROWID + ", "
+ Items.KEY_EMAIL + ", " + Items.KEY_NAME + ", "
+ Items.KEY_KURUMID + ", " + Items.KEY_BOLGEID + ", "
+ Items.KEY_ILID + " FROM " + Items.DATABASE_TABLE
+ " WHERE " + Items.KEY_BOLGEID + "= " + tempGroup
;
Cursor mCursor = db.rawQuery(sqlString, null);
return mCursor;
}
};
browseView.setAdapter(mAdapter);
}
我的结果应该是:
Group 1:
(db,"Asli", "asli@gmail.com", 1, 1, 1);
(db,"Osman Can", "osman@gmail.com", 1, 1, 1);
(db,"Abuzer Kadayif", "kadayif@gmail.com", 1, 2, 1);
Group 2:
(db,"Emre", "emre@gmail.com", 1, 3, 2);
(db,"Deniz", "deniz@gmail.com", 1, 4, 2);
Group 3:
(db,"Simon Garfunkel", "simon@gmail.com", 1, 5, 0);
(db,"Eric Clapton", "eric@gmail.com", 1, 6, 0);
(db,"Neil Young", "neil@gmail.com", 1, 5, 0);
但确实如此 第1组3次 第2组2 3次组0
我试图改变groupCursor的sql,但它崩溃了。
答案 0 :(得分:0)
您需要将第一个SQL语句更改为仅返回所需的组。现在你正在为每条记录获取它,所以适配器如果为每个记录提供一个组。将SQL更改为:
String sqlString = SELECT DISTINCT " + Items.KEY_BOLGEID + " AS _id FROM " + Items.DATABASE_TABLE;
这将为您提供您希望分组的每个项目的一个实例(并将其设置为_id列,这是适配器所必需的)。
然后在适配器调用的组部分中使用new String[] { _id }
。