I used to use content resolver previously to fetch Contact list and their details while that still works I wanted to try the Loader method which queries the Content Provider in the background. I have looked at the documentation and sample code here :
http://developer.android.com/training/contacts-provider/retrieve-details.html
While I was able to display a list of Contacts without any problems, I am stuck at retrieving Phone Number of a particular contact on clicking it and showing it on a detail screen. The demo above shows address of the user on the detail screen, I even tried modifying it. I have changed the content URI from Contacts to ContactsContract.CommonDataKinds.Phone but I keep getting
java.lang.IllegalArgumentException: Invalid column data1
There is not a single detailed tutorial on the internet showing how it is done. Can someone here be kind enough to shed some light? Reading various other Stackoverflow questions I am oretty sure it has something to do with the 2 different CONTENT URI but again I could be wrong. Any help or pointers would be really appreciated. Thanks
Few of the SO Questions I have already looked at :
Get phone number from contact loader in android: projection crash
Logcat says "invalid column data1"
How to get contacts' phone number in Android
Obtaining phone number from lookup URI
None of them solve my problem. Here is the relevant code where it fails :
final static String[] PROJECTION = {
Contacts._ID,
Utils.hasHoneycomb() ? ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY : ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
//ContactsContract.CommonDataKinds.Phone.NUMBER,
};
The code works just fine, but the moment I remove the comment to fetch Phone Number it fails
In my case the problem was with URI.
I was using this:
Uri uri = ContactsContract.Contacts.CONTENT_URI;
and I changed for this (Phone.CONTENT_URI) to resolve the problem and obtain the phone number:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
This is the rest of the code:
contactos = (TextView) findViewById(R.id.contactos);
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String [] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
while (cursor.moveToNext()){
String nombre = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String telefono = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactos.append("Name: ");
contactos.append(nombre);
contactos.append(" - Phone Number: ");
contactos.append(telefono);
contactos.append("\n");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With