Cursor cursor = resolver.query(
    Data.CONTENT_URI,
    DataQuery.PROJECTION,
    DataQuery.SELECTION,
    new String[] {String.valueOf(rawContactId)},
    null);投影为:
public static final String[] PROJECTION = new String[] {
    Data._ID,
    Data.MIMETYPE,
    Data.DATA1,
    Data.DATA2,
    Data.DATA3};选择是:
public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";
rawcontactId确实返回值,我已经做了日志来检查。为了给它提供一些上下文,我正在使用帐户同步。这里的目标是找到现有联系人的数据列,并用任何新数据覆盖它们。我正在使用安卓提供的以下示例代码:http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/platform/ContactManager.html
总结一下我的问题,我有两个联系人通过这个同步帐户添加,没有任何问题,但无法更新。有人有这方面的经验吗?谢谢。
编辑:这是我的rawContact返回方法
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;
}我得到的数字大概是3061。下面是UserIdQuery类:
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 + "=?";
}下面是用于添加新联系人的ContactSyncOperations类的构造函数。这里的源id是一个用户名,与我在选择参数中调用的相同。
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());
}谢谢!
发布于 2011-03-21 07:20:35
lookupRawContactId方法中有一个错误,我得到的rawcontactId long不是正确的。它应该看起来像这样:
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;
} https://stackoverflow.com/questions/5288384
复制相似问题