Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

android - Cannot retrieve email from Contacts

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 emails are not retrieving.

Here is the relevant code:

public  void getAllContacts(ContentResolver cr) {
          String email="";
            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.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();

         }

I know this question has been asked previously but none of the solutions has worked for me.So can anybody point me out the mistake I am doing here??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem. I solved it by changing kgandroid's code

String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));

to

String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

(Note that ContactsContract.CommonDataKinds.Phone.CONTACT_ID = "contact_id" while ContactsContract.Contacts._ID = "_id")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...