Create the Data Source Method

The complex list displays and uses several bits of data about a contact. To function properly, it needs all the data for a contact. This requires a method to retrieve contact data for all contacts from the database. Open or switch to ContactDataSource.java to create a new method that returns that data as Contact objects in an ArrayList. Use the code in Listing 6.5.

Listing 6.5 getContacts Method

public ArrayList<Contact> getContacts() {    ArrayList<Contact> contacts = new ArrayList<Contact>();    try {        String query = "SELECT  * FROM contact";        Cursor cursor = database.rawQuery(query, null);        Contact newContact;        cursor.moveToFirst();        while (!cursor.isAfterLast()) ...

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.