Hi I am currently trying to implement WhatsApp with an application I am working on. I want to be able to display all the WhatsApp contacts that a user has along with their phone number. So far I have been able to grab the name of their WhatsApp contact but I have not been able to grab the phone number of their WhatsApp contact, here is the code that I am using to get the list of a users WhatsApp contacts.
cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {
ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY
}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[] {
"com.whatsapp"
}, null);
int contactNameColumn = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
while (cursor.moveToNext()) {
String name = cursor.getString(contactNameColumn);
System.out.println(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
Integer.toString(contactNameColumn)
}, null);
while (pCur.moveToNext()) {
String phonenumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("PHONE: " + phonenumber);
}
pCur.close();
}
cursor.close();
When I print out the name it prints it out correctly, I then use that id to query the data table in order to get their phone number, but that always returns null
, can anyone help me out please.
Thanks
Change your
pCur
like thisAnd it is better to use proper naming conventions for variables. Why because here the varible
name
represents the id of the conatact.