views:

505

answers:

2

Hi,

I am new to Android and have been working on an app that needs to get all of the user's contact's phone numbers. Apparently the code I have does not work with the 2.1 SDK. So far here is the code I am using:

String[] projection = new String[] { Phone.NUMBER };
Cursor c = managedQuery( Phone.CONTENT_URI, projection, null, null, null );
int colIndex = -1;
try {
    colIndex = c.getColumnIndexOrThrow( Phone.NUMBER );
} catch( Exception e ) {
    print( e.getMessage() ); 
}

print( "Column Index = " + colIndex ); 

//count is equal to 3
for( int i = 0; i < count; i++ ){
    try {
        print( c.getString( 2 ) ); //the 2 used to be colIndex
    } catch ( Exception e ) {
        print( e.getMessage() ); 
    }
}

It seems that no matter what I pass into c.getString() it keeps telling me that I passed in -1. But I even hardcoded the 2, and it says the same thing. Any help would be much appreciated.

A: 

here is a good tutorial how to use the contact API

http://developer.android.com/resources/articles/contacts.html

For your specific question see the accepted answer on this question:

http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android

Roflcoptr
Okay thanks for the references. I'll take a look and tell you how it went.
Brandon Delany
Hey Sebi, The answer to that person's question didn't help. The answer and the question both used the deprecated API, pre-2.1. Could you just point me to what I should send into Cursor.getColumnIndex(...)?
Brandon Delany
Hi Brandon, i think you should use ContactsContract.PhoneLookup.DISPLAY_NAME or ContactsContract.CommonDataKinds.Phone.* please refer http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html
Vamsi
+1  A: 

Please check the following link on how to use the Android 2.0 Contacts API.
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

HTH !

Karan