安卓:访问和查询正确使用Raw联系人标识 [英] Android: Accessing and querying properly using Raw Contact Id

查看:314
本文介绍了安卓:访问和查询正确使用Raw联系人标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的应用程序试图将同步适配器集成到Android原生的联系人管理。这是所有除一次接触同步运行平稳,我无法更新。在这个特别的问题,详情可以在这里找到:<一href=\"http://stackoverflow.com/questions/5288384/android-content-resolver-query-returning-0-rows-when-it-ought-not-too/\">Android:内容解析器查询返回0行时,它不应该太... 但我可以随便说我的内容解析器查询将返回0值,因为我查询了错误的URI,我相信只是总结一下。

So my app is attempting to integrate a sync adapter to the android native contact manager. This is all running smoothly except once a contact is synced, I am unable to update it. Details on this particular problem can be found here: Android: Content resolver query returning 0 rows when it ought not too... But I can sum it up simply by just saying my content resolver query is returning 0 values because I am querying the wrong URI, I believe.

在我写的原料接触ID到手机上,我用下面的code做到这一点:

When I write the raw contact id to the phone, I do it with the following code:

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

当您添加新的联系人同步列表此构造方法叫做:

This constructor method is called when you are adding a new contact to the sync list:

private static void addContact(Context context, String account, Contact contact, BatchOperationForSync batchOperation) {
    ContactSyncOperations contactOp = ContactSyncOperations.createNewContact(context, contact.getUsername(), account, batchOperation);//constructor called @ this line
    contactOp.addName(contact.getFirstName(), contact.getLastName());
    contactOp.addEmail(contact.getEmail());
    contactOp.addPhone(contact.getPhoneNumber(), Phone.TYPE_MOBILE);
    contactOp.addPhone(contact.getHomePhone(), Phone.TYPE_HOME);
    contactOp.addPhone(contact.getWorkPhone(), Phone.TYPE_WORK);
    contactOp.addProfileAction(contact.getUsername());
    Log.e("Adding contact", "Via sync");
}

你可以在构造函数中看到,我调用了一个名为newInsertCpo方法,在这里可以查看:

As you can see in the constructor, I am calling a method called newInsertCpo, which can be viewed here:

private void addInsertOp() {
    if(!mIsNewContact) {
        mValues.put(Phone.RAW_CONTACT_ID, mRawContactId);
    }
    mBuilder = newInsertCpo(addCallerIsSyncAdapterParameter(Data.CONTENT_URI), mYield);
    mBuilder.withValues(mValues);
    if(mIsNewContact) {
        mBuilder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference);
    }
    mYield = false;
    mBatchOperation.add(mBuilder.build());
}

现在,你可以看到code,让我解释一下这个问题。当我创建和写作的原始接触ID,我做这么RawContacts.CONTENT_URI:

Now that you can see the code, let me explain the problem. When I am creating and writing the raw contact id, I am doing so to RawContacts.CONTENT_URI:

mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues);

然而,当我查询URI,我查询像这样:

However, when I query the uri, I am querying as so:

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

从Data.CONTENT_URI。我相信这是我的问题发生。我使用的示例code来自Android的官方开发站点和定制的为我自己的用途,但这一部分是pretty真实的例子。然而,这是行不通的。我的领导就是我说的,我正在写一个URI和查询另一个。但我已经尝试改变查询RawContacts.CONTENT_URI(造成的除外),同时也改变了乌里我写Data.CONTENT_URI,这也导致异常。什么是更令人困惑的是,我得到的原始接触IDS从Data.CONTENT_URI当我做我的lookupRawContactId方式:

From Data.CONTENT_URI. I believe this is where my problem is occurring. I used the sample code from Android's official dev site and tailored it for my own uses, but this part is pretty true to the example. Yet it is not working. My lead is what I said, I'm writing to one Uri and querying another. But I've attempted to change the query to RawContacts.CONTENT_URI (which caused an exception), and also changing the Uri I write to Data.CONTENT_URI, which also causes an exception. What's even more confusing is that I get raw contact ids from Data.CONTENT_URI when I do my lookupRawContactId 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;
}

所以啊,如果我得到一个返回游标rawcontactId,为什么我会得0值查询与同一rawcontactId同一个URI我被退回?它没有任何意义!没有人有任何见解?感谢所有。

So yea, if I get a cursor returning rawcontactId, why would I get 0 values querying that same Uri with that same rawcontactId i was returned? It doesn't make any sense! Does anyone have any insight? Thanks all.

推荐答案

该lookupRawContactId方法应该是这样,而不是:

The lookupRawContactId method should look like this instead:

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

注意查询而不是搜索RawContacts.CONTENT_URI。

Notice the query is searching RawContacts.CONTENT_URI instead.

这篇关于安卓:访问和查询正确使用Raw联系人标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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