【问题标题】:Retrieval of firstname and lastname from android contacts results in '1' and 'null'从 android 联系人中检索名字和姓氏会导致 '1' 和 'null'
【发布时间】:2012-12-06 18:13:34
【问题描述】:

我正在使用以下代码从 android 联系人中检索名字和姓氏。DISPLAY_NAME 返回联系人的姓名,而 firstname 和 lastname 分别返回 1 和 null。以下是代码。

ContentResolver cr = 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));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String firstname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    String lastname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    System.out.println("firstname and lastname" + firstname+ " "+lastname);
                    phoneNo = phoneNo.replaceAll("\\D", "");
                    names.add(name);
                    phnno.add(phoneNo);
                }
                pCur.close();
            }
        }           
    }

当我将 cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI 行更改为 cr.query(ContactsContract.Data.CONTENT_URI) 时,我得到的日志为

非常感谢您的建议。在此先感谢

【问题讨论】:

  • 这些可能是链接的联系人,并且可能没有附加姓名吗?
  • 我在编写代码之前已经亲自添加了它们。它们都附有名称
  • 您找到解决方案了吗?我遇到了同样的问题

标签: android contactscontract


【解决方案1】:

以下代码将帮助您获取名字和姓氏:

Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
Cursor nameCursor = getActivity().getContentResolver().query(
        dataUri,
        null,
        Data.MIMETYPE+"=?",
        new String[]{ StructuredName.CONTENT_ITEM_TYPE },
        null);
           while (nameCursor.moveToNext())
            {

                String      firstName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA2));
                String  lastName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA3));

                Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();

                return new String [] {firstName , lastName};
            }

           nameCursor.close();

【讨论】:

  • 谢谢,经过大量搜索,这是唯一对我有用的解决方案。常量有“DATA1”和“DATA2”之类的名称而不是更直观或更有意义的名称,这太糟糕了。
  • 你应该使用它而不是 DATA1、DATA2 等。developer.android.com/reference/android/provider/…
  • 乔伊森,谢谢。这真的很有帮助。只有这段代码对我有用
猜你喜欢
  • 2012-10-11
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多