Skip to content

Instantly share code, notes, and snippets.

@scottferg
Created May 14, 2010 20:06
Show Gist options
  • Save scottferg/401594 to your computer and use it in GitHub Desktop.
Save scottferg/401594 to your computer and use it in GitHub Desktop.
// List which columns we want to retrieve
String[] projection = new String[] {
Phones._ID,
Phones.NAME,
Phones.NUMBER
};
// Query the content provider
Cursor managedCursor = managedQuery(Phones.CONTENT_URI, // URI path
projection, // Which columns to fetch
null, // Which rows to return (null for all)
null, // Selection arguments
Phones.NAME + " ASC"); // Return results in ascending order by name
if (managedCursor.moveToFirst()) {
String name;
String number;
int nameIndex = managedCursor.getColumnIndex(Phones.NAME);
int numberIndex = managedCursor.getColumnIndex(Phones.NUMBER);
// We need to track which contact we're looking at
// so we know when to write out the result array
String currentName = null;
ArrayList<String> phoneNumberList = new ArrayList();
do {
// Pull out the contact's name and number
name = managedCursor.getString(nameIndex);
number= managedCursor.getString(numberIndex);
// Are we on a new contact? Then write out the current one
// and prepare a new one
if (! name.equals(currentName)) {
if (currentName != null) {
result.add(new Contact(currentName, phoneNumberList));
}
currentName = name;
phoneNumberList = new ArrayList();
}
phoneNumberList.add(number);
} while (managedCursor.moveToNext());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment