expandableList problem
I'm trying to build expandableList with SimpleCursorTreeAdapter but my list does not make groups, it repeats ever开发者_StackOverflow中文版y child element as group element.
My datas are formed as KEY_ROWID ,KEY_NAME , KEY_EMAIL ,KEY_ILID ,KEY_KURUMID ,KEY_BOLGEID
my datas:
(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);
And This is my code:
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);
}
My result should be:
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);
But it is 3 times group 1 2 times group 2 3 times group 0
And I tried to change sql of groupCursor, but it crashes.
You need to change your first SQL statement to only return the groups you want once. Right now you are getting it for each record, so the adapter if giving you a group for each one. Change your SQL to:
String sqlString = SELECT DISTINCT " + Items.KEY_BOLGEID + " AS _id FROM " + Items.DATABASE_TABLE;
That will get you one instance of each item you wish to group on (and set it to the _id column, which is required for the adapter).
Then just use new String[] { _id }
in the group portion of your adapter call.
精彩评论