0

Androidの連絡先の詳細からメールアドレスと電話番号を取得したいのですが、電話番号を取得することはできますが、電話番号を取得するときに、対応する連絡先IDにメールアドレスを取得したいので、arraylistに追加できます。ありがとう

public void getContactslist()
     {
         ContentResolver contactResolver = getContentResolver(); 
         Cursor cursor = contactResolver.query(Phone.CONTENT_URI , null,null,null, null);
        // Cursor emails = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,null, null, null); 
         if(cursor.getCount() > 0)
         {
             cursor.moveToFirst();
             do {
                String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                String displayname = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
                long   contactid   =   cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID));
                Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactid, null, null); 
                System.out.println("**emails*******"+number);
                while (emails.moveToNext()) 
                { 
                    String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                }
             }while (cursor.moveToNext() ); 
         }
     }
4

1 に答える 1

0

以下のスニペットを試してみましたが、うまくいきます。これがお役に立てば幸いです。

public HashMap<String, String> getNameEmailDetails(){
    HashMap<String, String> names = new HashMap<String, String>();
    ContentResolver cr = getContext().getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (cur1.moveToNext()) {
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);
                if(email!=null){
                    names.put(name, email);
                }
            }
            cur1.close();
        }
    }
    return names;
}
于 2016-05-09T10:29:40.840 に答える