安卓:内容解析器查询返回0行,当它不应该 [英] Android: Content resolver query returning 0 rows when it ought not to

查看:102
本文介绍了安卓:内容解析器查询返回0行,当它不应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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};

和选择之中:

公共静态最后弦乐选择= Data.RAW_CONTACT_ID +=?;

在rawcontactId不会返回值,我做了日志来检查。为了给它一些方面,我正在与客户同步。这里的目标是为它找到现有的联系人,写在他们与任何新数据的数据列。我是从搭载Android提供了以下示例code工作:<一href="http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/platform/ContactManager.html" rel="nofollow">http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/platform/ContactManager.html

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.

编辑:这是我的rawContact回归方法

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;
}

我回去就像是3061.这里的数字是UserIdQuery类:

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 + "=?";

}

这里是我构造一个ContactSyncOperations类被用来添加新的联系人。这里的源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());
}

谢谢!

推荐答案

有没有在lookupRawContactId方法的错误,rawcontactId长,我越来越不正确的。它应该是这个样子的:

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行,当它不应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆