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

2

4
Cursor cursor = resolver.query(
    Data.CONTENT_URI,
    DataQuery.PROJECTION,
    DataQuery.SELECTION,
    new String[] {String.valueOf(rawContactId)},
    null);

With PROJECTION being:

public static final String[] PROJECTION = new String[] {
    Data._ID,
    Data.MIMETYPE,
    Data.DATA1,
    Data.DATA2,
    Data.DATA3};

and SELECTION being:

public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";

The rawcontactId does return values, I've made logs to check. To give it some context I'm working with Account sync. The goal here is for it to find the data columns for existing contacts and writing over them with any new data. I'm working from the following sample code provided by android: http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/platform/ContactManager.html

To summarize my problem, I have two contacts via this synced account which are added without any problems, but are not being able to be updated. Anyone have experience with this? Thanks.

EDIT: Here is my rawContact returning method

private static long lookupRawContact(ContentResolver resolver, String username) {
    Log.e("Looking up Raw Contact", username);
    long authorId = 0;
    Cursor cursor = resolver.query(
        Data.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;
}

The numbers I get back are like 3061. Here is the UserIdQuery class:

final private static class UserIdQuery {

    private UserIdQuery() {

    }

    public final static String[] PROJECTION = new String[] {RawContacts._ID};
    public final static int COLUMN_ID = 0;
    public static final String SELECTION = RawContacts.ACCOUNT_TYPE + "='" + 
        "com.tagapp.android" + "' AND " + RawContacts.SOURCE_ID + "=?";

}

And here is my constructor for a ContactSyncOperations class being used to add a new contact. The source id here is a username, the same as I call in my SELECTION argument.

public ContactSyncOperations(Context context, String username,
        String accountName, BatchOperationForSync batchOperation) {

    this(context, batchOperation);
    mBackReference = mBatchOperation.size();
    mIsNewContact = true;
    mValues.put(RawContacts.SOURCE_ID, username);
    mValues.put(RawContacts.ACCOUNT_TYPE, "com.tagapp.android");
    mValues.put(RawContacts.ACCOUNT_NAME, accountName);
    mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues);
    mBatchOperation.add(mBuilder.build());
}

Thanks!

Cabalism answered 13/3, 2011 at 8:54 Comment(4)
You sure that you are using the RawContactId itself? Because the code looks fine and i dint had any issues implementing the same. Make sure if the value {String.valueOf(rawContactId)} is indeed the RawContactId and not ContactId. Any further reference to your code will help to answer.Pyromorphite
I will add the code for getting the rawContactId in a moment.Cabalism
whats the value of UserIdQuery.COLUMN_ID ?Pyromorphite
I added the UserIdQuery class at the bottom thereCabalism
C
2

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;
} 
Cabalism answered 20/3, 2011 at 23:20 Comment(0)
P
2

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}
    
Pyromorphite answered 13/3, 2011 at 11:30 Comment(3)
Ah yea... SOURCE_ID in the sample app was a userId from the contact manager being synced. I didn't have one so I chose username to be the identifier. I'll have a look at it and post a result, thanks for the tip!Cabalism
It looks like the source_id isn't the issue, I posted the code in which I set the ID up above. I'm going to try changing from Data.CONTENT_URI to RawContacts.CONTENT_URI and see how it goes. Thanks!Cabalism
I got the following error from changing it to RawContacts.CONTENT_URI 03-13 14:36:25.824: ERROR/DatabaseUtils(2456): java.lang.IllegalArgumentException: Invalid column mimetypeCabalism
C
2

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;
} 
Cabalism answered 20/3, 2011 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.