Getting RawContact id using Contact id

无人久伴 提交于 2019-12-03 08:46:42

You need to move your Cursor at first position. By default the cursor set at -1 index, so moving it to 0th position you need to use moveToFirst().


So you can modify your code like

Cursor c = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,projection,selection,selectionArgs , null);    
if (c.moveToFirst()) {    
    int rawContactId=c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
}
public static String getRawContactId(String contactId)
{
    String res = "";
    Uri uri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[]{ contactId };
    Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if(c != null && c.moveToFirst())
    {
        res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
        c.close();
    }

    return res;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!