联系人的身份证号码

问题描述:

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

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(); 

但是,如果我试图找到她的号码她的ID,

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 
}; 

第二光标是空的,即使我知道有在接触这样的条目。我究竟做错了什么?

+0

我记得有一个内容过滤器uri与电话号码查询。 – 2012-02-22 06:04:04

+0

所以你说:ContactsContract.CommonDataKinds.Phone.NUMBER!= ContactsContract.PhoneLookup.NUMBER(这是我不知道的) – 0x5f3759df 2012-02-22 06:06:18

不知道为什么,但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 
};