Android: Content resolver query returning 0 rows w

2019-02-15 17:26发布

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!

2条回答
对你真心纯属浪费
2楼-- · 2019-02-15 17:39

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;
} 
查看更多
男人必须洒脱
3楼-- · 2019-02-15 17:49

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}
    
查看更多
登录 后发表回答