Android设备联系人显示重复的联系人条目

4

我打算开发一个安卓应用程序,可以方便地获取设备联系人并以列表形式显示。

我使用以下代码来获取设备联系人,它可以正常工作,但会显示重复的联系人条目。

//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个回答

3

2
我将联系人插入数据库,其中我创建的表具有唯一的姓名和电话。因此消除了重复。 - Hoan Nguyen
2
我们能否不使用数据库呢?因为我需要使用联系人并且在我的情况下将联系人插入数据库会非常耗时。请给我一些其他的方法。 - Ricky Khatri
也许你可以使用IN_VISIBLE_GROUP进行过滤,我从未尝试过,所以不确定。 - Hoan Nguyen
我已经使用了IN_VISIBLE_GROUP。它删除了不必要的联系人,但设备联系人仍在重复。 - Ricky Khatri
我不知道你是否能够构建一个可以返回唯一姓名和电话的查询。我尝试了很长时间,最终放弃了,只是插入到数据库中以获得唯一性。虽然我可以循环比较,但我认为使用数据库更好。 - Hoan Nguyen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接