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

前端 未结 2 505
野性不改
野性不改 2021-01-07 07:34
Cursor cursor = resolver.query(
    Data.CONTENT_URI,
    DataQuery.PROJECTION,
    DataQuery.SELECTION,
    new String[] {String.valueOf(rawContactId)},
    null);
         


        
相关标签:
2条回答
  • 2021-01-07 07:41

    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}
      
    0 讨论(0)
  • 2021-01-07 07:59

    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;
    } 
    
    0 讨论(0)
提交回复
热议问题