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

Android设备联系人显示重复的联系人条目,第1张

概述我打算开发一个 Android应用程序,便于获取设备联系人显示在列表中. 我使用下面的代码获取设备联系人它工作正常,但显示重复的联系人条目. //FETCH DEVICE CONTACTSpublic void fetchDeviceContacts(){ Constant.progressDialog = ProgressDialog.show(ContactListActivit 我打算开发一个 Android应用程序,便于获取设备联系人并显示在列表中.

我使用下面的代码获取设备联系人它工作正常,但显示重复的联系人条目.

//FETCH DEVICE CONTACTSpublic 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_@R_404_6889@ + " 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 nick@R_404_6889@="";            String homePhone="";            String mobilePhone="";            String workPhone="";            byte[] photoByte=null;            String homeEmail="";            String workEmail="";            String company@R_404_6889@="";            String title="";            if(dataCursor.movetoFirst()){                // Getting display @R_404_6889@                contact@R_404_6889@= dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.disPLAY_@R_404_6889@ ));                do{                    // Getting Nick@R_404_6889@                    if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nick@R_404_6889@.CONTENT_ITEM_TYPE))                        nick@R_404_6889@ = 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)){                        company@R_404_6889@ = 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(contact@R_404_6889@!=null){                        _contactBean.set@R_404_6889@(contact@R_404_6889@);                    }                    if(photopath!=null){                        _contactBean.setPhotopath(photopath);                    }if(emailID!=null){                        _contactBean.setEmailID(emailID);                    }                    if(contact@R_404_6889@!=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(nick@R_404_6889@ != null && !nick@R_404_6889@.equals("") )                            details += "Nick@R_404_6889@ : " + nick@R_404_6889@ + "\n";                        if(homeEmail != null && !homeEmail.equals("") )                            details += "HomeEmail : " + homeEmail + "\n";                        if(workEmail != null && !workEmail.equals("") )                            details += "WorkEmail : " + workEmail + "\n";                        if(company@R_404_6889@ != null && !company@R_404_6889@.equals("") )                            details += "Company@R_404_6889@ : " + company@R_404_6889@ + "\n";                        if(Title != null && !Title.equals("") )                            details += "Title : " + Title + "\n";*/                // Adding ID,display @R_404_6889@,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设备联系人显示重复的联系人条目所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127403.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存