Sort the Contacts List

The next task is to code the ContactListActivity to sort the contact list according to the preferences set by the user. The first step is to modify the getContacts method in ContactDataSource. The method needs to be modified to accept the sort field and sort order as parameters and use these parameters in the SQL to perform the actual sort. Switch to ContactDataSource.java and locate the getContacts method. Change the message signature from

public ArrayList<Contact> getContacts() {

to

public ArrayList<Contact> getContacts(String sortField, String sortOrder) {

Next, change the SQL statement from

String query = "SELECT * FROM contact ...

Get Learning Mobile App Development: A Hands-on Guide to Building Apps with iOS and Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.