Cannot retrieve email from Contacts

前端 未结 3 1080
天涯浪人
天涯浪人 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:20

    Try this code:

    public  void getAllContacts(ContentResolver cr) {
        String email="";
        // Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, Phone.DISPLAY_NAME + " ASC");
        Cursor phones = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, Phone.DISPLAY_NAME + " ASC");
        //email = new String[phones.getCount()];
    
         while (phones.moveToNext()){
            String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println("name.................."+name+"........number......."+phoneNumber); 
    
            name1.add(name);
            phno1.add(phoneNumber);
    
            Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null
            , ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"
            , new String[]{id}, null);
            if(emailCur!=null){
                emailCur.moveToFirst();
            }
    
            // emailCur.moveToFirst();
            while (emailCur.moveToNext()) { 
                email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                System.out.println(email);
            } 
            email1.add(email); 
            emailCur.close();
        }
        phones.close();
    }
    

提交回复
热议问题