联系人的身份证号码

时间:2012-02-21 18:54:32

标签: android cursor contactscontract phone-number

我可以从她的身份证上找到联系人的电话号码。以下代码段在屏幕上打印一个数字。

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();

但是,如果我试图从她的号码中找到她的身份证,

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
            null, null);
    phones.moveToNext();
    String phoneNumber = phones.getString(
            phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //Now I got a valid phone number


            //using that number to find the id
    Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
            null, null);
    ids.moveToNext();
    String id = ids.getString(
            ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
    Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();

我收到以下错误。

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

我使用的投影如下:

private static final String[] CONTACT_PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    ContactsContract.CommonDataKinds.Phone._ID
};

即使我知道联系人中有这样的条目,第二个游标也是空的。我做错了什么?

1 个答案:

答案 0 :(得分:0)

不知道为什么,但PhoneLookup为我工作。对于那些有同样问题的人,这是我的代码。

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("aNumber"));
Cursor phones = getContentResolver().query(contactUri , PEOPLE_PROJECTION, null, null, null);
//here, the cursor keeps the wanted contact

...

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.PhoneLookup._ID
};