2013-03-14 171 views
4

我打算開發一個android應用程序,它有助於獲取設備聯繫人並顯示在列表中。Android設備聯繫人顯示重複的聯繫人條目

我使用下面的代碼來獲取設備聯繫人它工作正常,但顯示聯繫人的重複條目。

//FETCH DEVICE CONTACTS 
public void fetchDeviceContacts(){ 

    Constant.progressDialog = ProgressDialog.show(ContactListActivity.this,"", "Please wait..."); 

    Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; 
    // Querying the table ContactsContract.Contacts to retrieve all the contacts 
    final Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null, 
      ContactsContract.Contacts.DISPLAY_NAME + " ASC "); 

    if(contactsCursor.moveToFirst()){ 
     do{ 
      long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID")); 

      Uri dataUri = ContactsContract.Data.CONTENT_URI; 

      // Querying the table ContactsContract.Data to retrieve individual items like 
      // home phone, mobile phone, work email etc corresponding to each contact 
      Cursor dataCursor = getContentResolver().query(dataUri, null, 
        ContactsContract.Data.CONTACT_ID + "=" + contactId, 
        null, null); 

      String nickName=""; 
      String homePhone=""; 
      String mobilePhone=""; 
      String workPhone=""; 
      byte[] photoByte=null; 
      String homeEmail=""; 
      String workEmail=""; 
      String companyName=""; 
      String title=""; 

      if(dataCursor.moveToFirst()){ 
       // Getting Display Name 
       contactName= dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
       do{ 

        // Getting NickName 
        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)) 
         nickName = dataCursor.getString(dataCursor.getColumnIndex("data1")); 

        // Getting Phone numbers 
        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){ 
         switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){ 
         case ContactsContract.CommonDataKinds.Phone.TYPE_HOME : 
          homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1")); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE : 
          mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1")); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_WORK : 
          workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1")); 
          break; 
         } 
        } 

        // Getting EMails 
        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { 
         switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){ 
         case ContactsContract.CommonDataKinds.Email.TYPE_HOME : 
          emailId =homeEmail= dataCursor.getString(dataCursor.getColumnIndex("data1")); 
          break; 
         case ContactsContract.CommonDataKinds.Email.TYPE_WORK : 
          emailId=workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1")); 
          break; 
         } 
        } 

        // Getting Organization details 
        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){ 
         companyName = dataCursor.getString(dataCursor.getColumnIndex("data1")); 
         title = dataCursor.getString(dataCursor.getColumnIndex("data4")); 
        } 

        // Getting Photo 
        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){ 
         photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15")); 

         if(photoByte != null) { 
          Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length); 

          // Getting Caching directory 
          File cacheDirectory = getBaseContext().getCacheDir(); 

          // Temporary file to store the contact image 
          File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png"); 

          // The FileOutputStream to the temporary file 
          try { 
           FileOutputStream fOutStream = new FileOutputStream(tmpFile); 

           // Writing the bitmap to the temporary file as png file 
           bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream); 

           // Flush the FileOutputStream 
           fOutStream.flush(); 

           //Close the FileOutputStream 
           fOutStream.close(); 

          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
          photoPath = tmpFile.getPath(); 

         } 
        } 


        //CREATE CONTACT LIST 
        _contactBean=new ContactBean(); 
        if(contactName!=null){ 
         _contactBean.setName(contactName); 
        } 
        if(photoPath!=null){ 
         _contactBean.setPhotoPath(photoPath); 
        }if(emailId!=null){ 
         _contactBean.setEmailId(emailId); 
        } 
        if(contactName!=null){ 
         _contactArrayList.add(_contactBean); 
        } 

        ////////////////////// 

       }while(dataCursor.moveToNext()); 
       /*String details = ""; 

         // Concatenating various information to single string 
         if(homePhone != null && !homePhone.equals("")) 
          details = "HomePhone : " + homePhone + "\n"; 
         if(mobilePhone != null && !mobilePhone.equals("")) 
          details += "MobilePhone : " + mobilePhone + "\n"; 
         if(workPhone != null && !workPhone.equals("")) 
          details += "WorkPhone : " + workPhone + "\n"; 
         if(nickName != null && !nickName.equals("")) 
          details += "NickName : " + nickName + "\n"; 
         if(homeEmail != null && !homeEmail.equals("")) 
          details += "HomeEmail : " + homeEmail + "\n"; 
         if(workEmail != null && !workEmail.equals("")) 
          details += "WorkEmail : " + workEmail + "\n"; 
         if(companyName != null && !companyName.equals("")) 
          details += "CompanyName : " + companyName + "\n"; 
         if(title != null && !title.equals("")) 
          details += "Title : " + title + "\n";*/ 

       // Adding id, display name, path to photo and other details to cursor 

      } 

     }while(contactsCursor.moveToNext()); 
    } 

    runOnUiThread(new Runnable() { 
     public void run() { 
      Constant.progressDialog.cancel(); 
      contactsCursor.close(); 
     } 
    }); 
} 

幫助將不勝感激。謝謝

回答

3

您看到重複,因爲同一聯繫人可能屬於不同的組。例如電子郵件帳戶1和電子郵件帳戶2.
您可以在http://developer.android.com/guide/topics/providers/contacts-provider.html

+2

獲得更多信息的副標題原始聯繫人數據的來源所以我怎麼可能顯示不同的接觸detalis。 – 2013-03-19 04:41:32

+2

我將聯繫人插入到我創建的表名稱和電話號碼唯一的數據庫中。從而消除重複。 – 2013-03-19 04:46:44

+2

我們可以做到這一點,而無需使用數據庫b'cas我必須做一些不同的任務使用聯繫人和插入聯繫人分貝將是非常沉重的時間消耗在我的情況。Plz給我一些其他方式。 – 2013-03-19 05:07:03

相關問題