Android People.Number and People.Number_key return null
I am trying to get the contacts from the phone but all I can get is the name, the phone numbers return null.
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");
startManagingCursor(cursor);
cursor.moveToFirst();
if(cursor.getCount() > 0){
while(cursor.moveToNext()){
int idCol = cursor.getColumnIndex(People._ID);
int nameCol = cursor.getColumnIndex(People.NAME);
int numCol = cursor.getColumnIndex(People.NUMBER_KEY);
String name = cursor.getString(nameCol);
String number = cursor.getString(numCol);
//mDbHelper.addContact(managedCursor.getString(1),managedCursor.getString(2));
Log.v("contact.add",name+" - "+number);
}
Any ideas?
After about 3 hours, I have figured it out:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
cur.move(-1);
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
Log.v("Phone",""+pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+" - "+name);
开发者_JS百科 i++;
}
pCur.close();
}
}
}
}
The column for phone number is People.NUMBER, not People.NUMBER_KEY.
精彩评论