My app lets user select a category and then input other info based on the category. Say they pick category ‘Electronics’ then they can input CD, PS3, iPod etc in it. I have several categories like Hobbies, General, Home etc.
I would like to display the items in a listview sorted by category like this:
Electronics
-CD
-PS3
-iPod
Books
-Perl
-Android is fun
-C
etc etc…
I am fetching all the data from the db using:
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_CATEGORY
KEY_NAME, KEY_STATUS, KEY_DESCRIPTION}, null, null, null, null, null);
i also tried using the rawQuery and using the DISTINCT tag but that’s not going to work, since it will pick unique items. I am using a simpleCursorAdapter to display the data:
SimpleCursorAdapter list =
new SimpleCursorAdapter(this, R.layout.items, c, mapFrom, mapTo);
setListAdapter(list);
I guessing that you want this:
Using the Order By parameter your query is first ordered by category then name. This would give you a result like:
If you only wanted to list Books, then you simply use:
Of course, if you only want to display the category and name in a ListView then you it will be more efficient to shorten you SELECT clause to only what you need:
new String[] {KEY_ROWID, KEY_CATEGORY, KEY_NAME}.Addition from comments
In order to get a result like this:
You can either use an ExpandableListView or process the Cursor yourself into a
List<String>and pass this to a ArrayAdapter.