0

我正在使用以下代码从 android 检索联系人的名字和姓氏列表,但是我也想同时检索电子邮件:

Cursor1 = getContentResolver().query(
           public List<String> getFullContactName()
           {
                List<String> name = new ArrayList<String>();
                String[] projection = new String[] {Data.DATA2, Data.DATA3};
                String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
                Uri uri = Data.CONTENT_URI;

                ContentResolver contentResolver = getApplicationContext().getContentResolver();
                Cursor cursor = contentResolver.query(uri,projection,where,null,null);
                String firstName, lastName;
                while (cursor.moveToNext())
                {
                    firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
                    lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
                    name.add(firstName + " " + lastName);     
                    Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();
                }

                cursor.close();
                cursor = null;
               return name;
            }
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
   while (emails.moveToNext()) { 
      // This would allow you get several email addresses 
      String emailAddress = emails.getString( 
      emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
   } 
   emails.close();
}
cursor.close(); 

我需要知道如何使用单个光标一起读取名字、姓氏电子邮件、电话号码,因为我需要在列表中获取详细信息,然后在列表视图中显示它们。我还没有参考资料,我可以使用单个光标获取详细信息。

4

2 回答 2

0

消除

cursor.close();
cursor = null;

并确保contactId您传递给 的Cursor emails与您从中检索的相同Cursor cursor似乎您没有在您的 String[] projection

对于第一个投影,您可以如下:

String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME};

此投影将检索联系人 ID 和联系人姓名,然后获取联系人 ID 和姓名

String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
 String Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

那么第二个投影可能是这样的:

String[] projection2= new String[]{
                                 Email.ADDRESS,                 
                                 Email.TYPE

然后定义第二个光标以在第一个光标内获取电子邮件,例如

Cursor cur2 = cr.query(emailURI,projection2,Email.CONTACT_ID + "=" +id,null,null);

其中 id 是您从第一个光标获得的 id。

希望这有帮助

于 2012-12-14T16:13:05.073 回答
0

使用以下代码获取联系方式。

public void getContacts(ContentResolver cr) {

    ArrayList<HashMap<String, String>> mContacts = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> mTempObj = new HashMap<String, String>();

    cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null, null, null, null);
    cur.moveToFirst();
    if (cur.getCount() > 0) {
        for (int i = 0; i < cur.getCount(); i++) {
            mTempObj.add("Email", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));

            cur.moveToNext();
        }
    }

    cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null, null);
    cur.moveToFirst();
    if (cur.getCount() > 0) {
        for (int i = 0; i < cur.getCount(); i++) {
            mTempObj.add("Number", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));

            cur.moveToNext();
        }
    }

    cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
            null);
    cur.moveToFirst();
    if (cur.getCount() > 0) {
        for (int i = 0; i < cur.getCount(); i++) {
            mTempObj.add("Name", cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

            cur.moveToNext();
        }
    }

    cur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
            null, null, null, null);
    cur.moveToFirst();
    if (cur.getCount() > 0) {
        for (int i = 0; i < cur.getCount(); i++) {
            mTempObj.add("Address", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
            mTempObj.add("City", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
            mTempObj.add("State", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));

            cur.moveToNext();
        }
    }
    mContacts.add(mTempObj);
}

它会解决你的问题。

于 2012-12-15T09:02:49.400 回答