colabug
colabug

Reputation: 2602

How do you specify the sort order for children in an expandable list?

I'm working on a conference application where we want the sessions to be first grouped by time and then by room location. I have successfully sorted by one or the other in my ExpandableListActivity, but have been unsuccessful with both the primary and secondary sort.

conference application that displays the sessions first grouped by time and then by room location
(source: coreylatislaw.com)

Set up

Query

    Cursor cursor = context.getContentResolver()
                           .query(uri,
                                  ScheduleData.PROJECTION,
                                  null,
                                  null,
                                  ScheduleData.SORT_ORDER);

Sort order

    public static final String SORT_ORDER = TimeSlots.QUALIFIED_TIMESTART + " ASC"; // timeslots.timestart

Failed primary & secondary sort orders

    public static final String SORT_ORDER = TimeSlots.QUALIFIED_TIMESTART + " ASC, " + Locations.QUALIFIED_NAME + " ASC"; // timeslots.timestart ASC, locations.name ASC
    public static final String SORT_ORDER = TimeSlots.QUALIFIED_TIMESTART + ", " + Locations.QUALIFIED_NAME + " ASC";     // timeslots.timestart, locations.name ASC

The second clause seems to have no affect on the ordering. Is this a limitation of the ExpandableListActivity? Should I specify multiple sort order items differently?

Upvotes: 4

Views: 2404

Answers (3)

colabug
colabug

Reputation: 2602

Turns out that there was a comparator in the class that was overriding the sort order specified in the ORDERBY clause. When using the ORDERBY clauses above with out the comparator, I got the desired sorting. You can do it either way, but I'm killing the extra code and choosing the ORDERBY clause.

The comparator path:

// Sort children
Collections.sort(group.children, CHILD_COMPARATOR);

// Specify sort by location
static final Comparator<Child> CHILD_COMPARATOR = new Comparator<Child>()
{
    @Override
    public int compare (Child child1, Child child2)
    {
        return child1.location.toLowerCase().compareTo(child2.location.toLowerCase());
    }
};

Upvotes: 1

Renard
Renard

Reputation: 6929

this is not really an answer, but i cant write comments yet. The SORT_ORDER String should be correct. But what you can try is to use the shell and execute the sql lite query directly so that you can narrow down the problem.

  1. start up emulator with your app
  2. in your console type :

    adb shell

  3. now navigate to the directory of your app; for example:

    cd /data/data/com.myapp/databases)

  4. start the SQLLite command line tool by typing:

    sqlite3 < filename >

  5. If you dont know the name of your database file simply type in "ls" do see all files in this directory.

  6. you can now type in your query. For example:

SELECT * FROM timeslots,locations ORDER BY timeslots.timestart, locations.name;

reference: http://www.sqlite.org/sqlite.html

Upvotes: 0

c0d3rguy
c0d3rguy

Reputation: 191

Just get the data into an 'ArrayAdapter', sort it the way you want and then set back the adapter to the activity list.

Upvotes: 0

Related Questions