Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android设备联系人显示重复的联系人条目大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我打算开发@L_197_0@ 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,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,ContactsContract.Data.CONTACT_ID + "=" + contactId,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,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获得有关原始联系人数据的子标题源的更多信息

大佬总结

以上是大佬教程为你收集整理的Android设备联系人显示重复的联系人条目全部内容,希望文章能够帮你解决Android设备联系人显示重复的联系人条目所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。