0

Possible Duplicate:
How to read contacts on Android 2.0

I just want a List<String> of first name + last name from a devices contact list in Android. Should be something really simple, but I can't find any basic code.

Just looking for:

Steve Smith
Joe Shmo
Blah Davis

etc. all from the contacts on a users phone. I've already added:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
Community
  • 1
  • 1
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

1 Answers1

1
    public List<String> getFullContactName(){

    List<String> name = new List<String>();

    String[] projection = new String[] {Data.DATA2, Data.DATA3};
    String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
    Uri uri = Data.CONTENT_URI;

    ContentResolver contentResolver = context.getContentResolver();
    // Make the query. 
    Cursor cursor = contentResolver.query(uri,
                             projection, // Which columns to return 
                             where,       // Which rows to return
                             null,       // Selection arguments (none)
                             // Put the results in ascending order by name
                             null);

    String firstName, LastName;
    while (cursor.moveToNext()) {

        firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
        lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);           
    }

    cursor.close();
    cursor = null;
    return name;
}
Hardik4560
  • 3,202
  • 1
  • 20
  • 31
  • Thanks, but this seems to be pulling a LOT more contacts into the list besides just the ones from my Phone app. Any way to get just the ones that appear in my Phone app? – Ethan Allen Oct 22 '12 at 20:56
  • If by phone app you mean native contact application then ya this piece of code brings contact from it. – Hardik4560 Oct 23 '12 at 09:25