2013-07-02 57 views
4

我需要從我的聯繫人列表的每個聯繫人中獲取姓名,電子郵件和電話號碼。從聯繫人列表中獲取用戶信息Android

問題是我可以得到姓名和電話號碼,但不是電子郵件。我正在收到電話號碼而不是電子郵件。

這裏是我的代碼:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null); 
     while (phones.moveToNext()) { 

      String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); 

      ContactItem objContact = new ContactItem(); 
      objContact.setName(name); 
      objContact.setPhoneNo(phoneNumber); 
      objContact.setEmail(email); 
      list.add(objContact); 

     } 
     phones.close(); 

回答

4
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    while (cursor.moveToNext()) { 
     String contactId = cursor.getString(cursor.getColumnIndex(
       ContactsContract.Contacts._ID)); 

     //May want to get basic info here like name, phone 
     //Example: 
     //Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
     //while (phones.moveToNext()) { 
     // String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     // Log.i("phone", phoneNumber); 
     //} 
     //phones.close(); 

     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
     while (emails.moveToNext()) { 
      String emailAddress = emails.getString(
        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 

      Log.i("emails", emailAddress); 
     } 
     emails.close(); 
    } 
    cursor.close(); 

來源和參考: How to read contacts on Android 2.0

+0

感謝分享+1。 – justLearning

0

公共無效onActivityResult(INT reqCode,INT resultCode爲,意圖數據){ 超。 onActivityResult(reqCode,resultCode,data);

switch (reqCode) { 
    case (PICK_CONTACT): 
     if (resultCode == Activity.RESULT_OK) { 
      String phoneNumber = null; 
      String name = null; 
      String emailAddress = null; 

      Uri contactData = data.getData(); 
      Cursor c = managedQuery(contactData, null, null, null, null); 

      if (c.moveToFirst()) { 
       Cursor cursor = null; 

       cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
       while (cursor.moveToNext()) { 
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
        name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
        // May want to get basic info here like name, phone 
        // Example: 
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, 
          null, null); 
        while (phones.moveToNext()) { 
         phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         Log.i("phone", phoneNumber); 
        } 
        phones.close(); 

        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, 
          null, null); 
        while (emails.moveToNext()) { 
         emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
         Log.i("emails", emailAddress); 
        } 
        emails.close(); 
       } 
       cursor.close(); 

       Log.i(DEBUG_TAG, "Got a contact result: " + name + emailAddress + "Phone Number is :- " + phoneNumber); 
       Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show(); 

      } 
     } 
    } 
} 
相關問題