3

我正在尝试从 ContactsContract 获取电子邮件地址,但我只得到一个空字符串!姓名和号码工作正常,但不是电子邮件地址!

我很困惑,几天来一直在尝试解决这个问题,但我不知道出了什么问题,如果我错过了代码中的某些内容或者我不知道如何解决这个问题。

应用程序的这一部分具有来自 ContactsContract 的所有名称的 ListView。在 ListView 中选择姓名时,代码应从 ContactsContract 中获取姓名、号码和电子邮件地址。

我将不胜感激能够继续工作的一些帮助!也许有更好的方法来解决这个问题,那么请告诉我如何改变!谢谢!

由于 toast 消息没有出现在 while 循环中,while(cursorEmail.moveToFirst()){....我猜电子邮件光标有问题!?好像是空的!?

public class Activity_3 extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_3);
    listView = (ListView) findViewById(R.id.contactList);

    String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone._ID };

    Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

    // From column
    String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
    // To view
    int[] toView = { R.id.contactItem };

    startManagingCursor(cursor1);

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

            String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, 
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    };

            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

            cursor.moveToPosition(position);

            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            String pos = Integer.toString(position);

            String contactEmailAddress = "?";   
            //Email
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null,  null);

            while(emails.moveToNext()){
                contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
            }
            emails.close();

            Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();
        }
    });
}

}

4

5 回答 5

11

您需要使用以下代码获取包含详细信息的联系人列表。以下方法将返回所有联系人的姓名、电话号码和电子邮件。

public ArrayList<ContactDO> getContacts()
{
    ArrayList<ContactDO> alContacts = null;
    Cursor cursor = contResv.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if(cursor.moveToFirst())
    {
        alContacts = new ArrayList<ContactDO>();
        do
        {
            //Create a plain class with following variables - id, name, contactNumber, email
            ContactDO objContactDO = new ContactDO();

            objContactDO.name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
            objContactDO.id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            Cursor emails = contResv.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + objContactDO.id, null, null);
            while (emails.moveToNext()) 
            { 
                objContactDO.email = emails.getString(emails.getColumnIndex(Email.DATA));
                break;
            }
            emails.close();

            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = contResv.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ objContactDO.id }, null);
                while (pCur.moveToNext()) 
                {
                    objContactDO.contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;
                }
                pCur.close();
            }

            alContacts.add(objContactDO);

        } while (cursor.moveToNext()) ;
    } 

    cursor.close(); 
    return alContacts;
}

创建一个自定义适配器并将其设置为 listview。

lvContacts.setAdapter(adapter = new CustomContactsAdapter(getContacts()));
于 2013-03-13T06:51:25.237 回答
3

假设您已经获得了您要为其获取电子邮件地址的特定联系人的 ID,这是我不久前尝试做的与您现在一样的代码片段:

首先,您需要获取联系人的 ID:

    ContentResolver cr  = context.getContentResolver();
    Cursor cursor       = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    id                  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

然后,您可以通过以下方式获取电子邮件地址:

            String contactAddress   = "";
            Cursor cursorEmail      = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                            null, 
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                            new String[] {id}, 
                                            null);
            if(cursorEmail.moveToFirst()) {
                contactAddress = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

请记住,联系人不一定有电子邮件,因此您当然应该考虑到这一点:-)

编辑:更好地查看您的代码并将其与我在此处粘贴的内容进行比较。似乎您的问题在于参数selectionselectionArgs. 您要做的是获取您要为其获取电子邮件地址的联系人的 ID,然后将您的选择更改为:

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"

并在 selectionArgs 中添加 id,如下所示:

new String[] {id}
于 2013-03-06T09:26:17.633 回答
0

用这个

String email = "";  
            try {  
                Uri result = data.getData();  
                Log.v(DEBUG_TAG, "Got a contact result: "  
                        + result.toString());  
                // get the contact id from the Uri  
                String id = result.getLastPathSegment();  
                // query for everything email  
                cursor = getContentResolver().query(Email.CONTENT_URI,  
                        null, Email.CONTACT_ID + "=?", new String[] { id },  
                        null);  
                int emailIdx = cursor.getColumnIndex(Email.DATA);  
                // let's just get the first email  
                if (cursor.moveToFirst()) {  
                    email = cursor.getString(emailIdx);  
                    Log.v(DEBUG_TAG, "Got email: " + email);  
                } else {  
                    Log.w(DEBUG_TAG, "No results");  
                }  
            } catch (Exception e) {  
                Log.e(DEBUG_TAG, "Failed to get email data", e);  
            } finally {  
                if (cursor != null) {  
                    cursor.close();  
                } 

               edtCustomerEmailid.setText(email);

              //Toast.makeText(getApplicationContext(), email, 1).show();
                if (email.length() == 0) {  
                    Toast.makeText(this, "No email found for contact.",  
                            Toast.LENGTH_LONG).show();  
                }  
            } 
于 2013-03-15T13:41:15.457 回答
0

在这里,我发布了一个工作方法,它将通过联系人 ID 返回邮件 ID 或 null(如果不可用)。

public static String getEmail(Context c, long id) {
        Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Email._ID,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID,
                ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.TYPE };
        String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID
                + " = '" + id + "'";
        String sortOrder = ContactsContract.CommonDataKinds.Email.ADDRESS
                + " COLLATE LOCALIZED ASC";

        Cursor cursor = c.getContentResolver().query(uri, projection,
                selection, null, sortOrder);

        int index = cursor
                .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
        if (cursor.moveToNext()) {
            return cursor.getString(index);
        }
        return null;
    }
于 2013-03-14T13:17:09.130 回答
0
ContentResolver cr = this.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));
            String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));



        } 
        cur1.close();
    }
}
于 2013-03-15T11:22:25.687 回答