Android: Content resolver query returning 0 rows when it ought not to

蹲街弑〆低调 提交于 2019-12-01 00:46:31
JMRboosties

There was an error in the lookupRawContactId method, the rawcontactId long I was getting wasn't the right one. It should have looked like this:

private static long lookupRawContact(ContentResolver resolver, String username) {
    Log.e("Looking up Raw Contact", username);
    long authorId = 0;
    Cursor cursor = resolver.query(
        RawContacts.CONTENT_URI,
        UserIdQuery.PROJECTION,
        UserIdQuery.SELECTION,
        new String[] {username},
        null);

    try {
        if(cursor != null && cursor.moveToFirst()) {
            authorId = cursor.getLong(UserIdQuery.COLUMN_ID);
        }
    } finally {
        if(cursor != null) {
            cursor.close();
        }
    }
    return authorId;
} 
Manish Khot

There are a few issues that i could locate with the following query:

Cursor cursor = resolver.query(Data.CONTENT_URI, 
                               UserIdQuery.PROJECTION, 
                               UserIdQuery.SELECTION, 
                               new String[] {username}, null);
  1. If all the columns are pointing out at RawContacts table then you should use RawContacts.CONTENT_URI instead of Data.CONTENT_URI.
  2. Here the value of RawContacts.SOURCE_ID is compared with username

    public static final String SELECTION = RawContacts.ACCOUNT_TYPE + "='" + 
            "com.tagapp.android" + "' AND " + RawContacts.SOURCE_ID + "=?";
    
    new String[] {username}
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!