2016-09-01 87 views
0

我使用下面的代碼通過電話號碼和電子郵件閱讀聯繫人!它會成功讀取所有聯繫人,但如果您聯繫沒有電子郵件,則會顯示上一個聯繫人的電子郵件。閱讀聯繫信息電話和電子郵件

就像下圖所示!有任何想法嗎?? 預先感謝您

public void getContacts() { 

    contactList = new ArrayList<Contacts>(); 
    String phoneNumber = null; 
    String email = null; 
    ContentResolver contentResolver = getContentResolver(); 


    String name = null; 

    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    // Iterate every contact in the phone 
    if (cursor.getCount() > 0) { 
     counter = 0; 

     while (cursor.moveToNext()) { 

      // Update the progress message 
      updateBarHandler.post(new Runnable() { 
       public void run() { 
        pDialog.setMessage("Reading contacts : " + counter++ + "/" + cursor.getCount()); 
       } 
      }); 

      String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 


      int hasPhoneNumber = Integer.parseInt(cursor.getString(
        cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 

       //This is to read multiple phone numbers associated with the same contact 
       Cursor phoneCursor = contentResolver.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{contact_id}, null); 

       while (phoneCursor.moveToNext()) { 
        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       } 
       phoneCursor.close(); 
      } 

      Cursor emailCursor = contentResolver.query(
        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
        ContactsContract.CommonDataKinds.Email.CONTACT_ID 
        + " = ?", new String[]{contact_id}, null); 

      while (emailCursor.moveToNext()) { 

       email = emailCursor.getString(emailCursor.getColumnIndex(
         ContactsContract.CommonDataKinds.Email.DATA)); 
      } 

      emailCursor.close(); 
      contact = new Contacts(); 

      contact.setContactFName(name); 
      contact.setContactPhone(phoneNumber); 
      contact.setContactEmail(email); 

      contactList.add(contact); 
     } 

     // ListView has to be updated using a ui thread 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       adapter = new ImportContactAdapter 
         (ImportContactActivity.this, R.layout.custom_import_contact, contactList); 
       mListView.setAdapter(adapter); 
      } 
     }); 
     // Dismiss the progressbar after 500 millisecondds 
     updateBarHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       pDialog.cancel(); 
      } 
     }, 500); 
    } 
} 

enter image description here

+0

可能重複[訪問聯繫人姓名,號碼和電子郵件ID?](http://stackoverflow.com/questions/10993283/accessing-the-contact-name-number-and-email-id) –

回答

0

這與你的變量的作用域做。您在while(cursor.moveToNext())之外聲明您的String email。當您的聯繫人沒有任何電子郵件時,while (emailCursor.moveToNext())將不會更新您的String email的值,這意味着當您執行contact.setContactEmail(email);時,它仍包含上一次聯繫人的值。