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();
        }
    });
}

的帮助将不胜感激。由于

您看到重复是因为相同的联系人可能属于不同的组。例如:邮箱帐号1,邮箱帐号2。
您可以从副标题获得更多信息原始联系人数据来源在http://developer.android.com/guide/topics/providers/contacts-provider.html

最新更新