安卓。内容解析器查询在不应该的情况下返回0行[英] Android: Content resolver query returning 0 rows when it ought not to

本文是小编为大家收集整理的关于安卓。内容解析器查询在不应该的情况下返回0行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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确实返回值,我已经进行了日志检查.为了给它一些上下文,我正在使用帐户同步.这里的目标是为了找到现有联系人的数据列,并使用任何新数据编写它们.我正在从Android提供的以下示例代码工作: http://developer.android.com/resources/samples/samplesyncadapter/src/src/com/com/example/android/samplesync/platform/contactmanager.html.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 + "=?";

}

这是我用于添加新联系人的Contactncoperations类的构造函数.这里的源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());
}

谢谢!

推荐答案

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

其他推荐答案

我可以在以下查询中找到一些问题:

Cursor cursor = resolver.query(Data.CONTENT_URI, 
                               UserIdQuery.PROJECTION, 
                               UserIdQuery.SELECTION, 
                               new String[] {username}, null);
  1. 如果所有列都在RawContacts表上指出,则应使用RawContacts.CONTENT_URI而不是Data.CONTENT_URI.
  2. 在这里将RAWCONTACTS.SOURCE_ID的值与用户名进行比较

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

本文地址:https://www.itbaoku.cn/post/102437.html

问题描述

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!

推荐答案

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

其他推荐答案

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}