Cannot retrieve email from Contacts

前端 未结 3 1073
天涯浪人
天涯浪人 2020-12-12 03:23

I want to retrieve email,phone number and contact names from my phone-book and display them in list-view.

The name and numbers are coming perfectly but the e

3条回答
  •  遥遥无期
    2020-12-12 04:12

    Try this way,hope this will help you to solve your problem.

        public ArrayList> getAllContacts(ContentResolver cr) {
            ArrayList> contactList = new ArrayList>();
            Cursor phones = cr.query(Phone.CONTENT_URI, null,null,null, Phone.DISPLAY_NAME+ " ASC");
    
            while (phones.moveToNext())
            {
                HashMap row = new HashMap();
                String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
                String name=phones.getString(phones.getColumnIndex(Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                System.out.println("name.................."+name+"........number......."+phoneNumber);
    
                row.put("Name",name);
                row.put("PhoneNo",phoneNumber);
                row.put("Email",getEmail(cr,id));
                contactList.add(row);
            }
    
    
            phones.close();
            return contactList;
        }
    
        private static String getEmail(ContentResolver cr,String contactId) {
            String emailStr = "";
            final String[] projection = new String[]{Email.DATA, Email.TYPE};
    
            final Cursor email = cr.query(Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?",
                    new String[]{contactId}, null);
    
            if (email.moveToFirst()) {
                final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);
    
                while (!email.isAfterLast()) {
                    emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
                    email.moveToNext();
                }
            }
            email.close();
            return emailStr;
        }
    
    Note : add this  in AndroidManifest.xml
    

提交回复
热议问题