由数据ID更新联系人数据 [英] Update Contact Data by Data ID

查看:142
本文介绍了由数据ID更新联系人数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用联系人的ContentProvider 来存储一些数据。当我打开一个接触到内存中,我要救它的 ID (让我知道以后怎么保存更改),以及 ID (因此它们可以直接更新)。下面是我的一些code的:

My application uses the Contacts ContentProvider to store some of its data. When I load a contact into memory, I want to save its ID (so that I know how to save changes later), and the ID of all data fields it is using (so they can be directly updated). Here is some of my code:

    Uri entityUri = Uri.withAppendedPath(
            ContentUris.withAppendedId(RawContacts.CONTENT_URI, id),
            Entity.CONTENT_DIRECTORY);

    Cursor resultData = context.getContentResolver().query(
            entityUri,
            new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
            null, null, null);
    resultData.moveToFirst();

    this.id = id;
    while (resultData.isAfterLast() == false) {
        this.source_id = resultData.getInt(0);
        if (!resultData.isNull(1)) {
            if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) {
                this.display_name = resultData.getString(3);
                this.display_name_id = resultData.getInt(1);
            }
        }
        resultData.moveToNext();
    }
    resultData.close();
    return this;

这是查询的ContentProvider 并从数据获取 DISPLAY_NAME 字段。数据记录的 ID 保存在 display_name_id 变量。它出来为 4612 时,我的设备上运行。

That queries the ContentProvider and gets the DISPLAY_NAME field from the data. The ID of the data record is stored in the display_name_id variable. It comes out as 4612 when I run it on my device.

我试图挽救它,但预期它不会更新。为了调试,我补充说,试图找到正确的数据表中记录的查询。

I tried saving it, but it does not update as expected. In order to debug, I added a query that tries to find the correct data table record.

    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            new String[] {ContactsContract.Data._ID, CommonDataKinds.StructuredName.DISPLAY_NAME},
            ContactsContract.Data._ID + "=?",
            new String[] {String.valueOf(this.display_name_id)}, null);

不过,这光标回来具有 0 的长度。这怎么可能?为什么是ID不正确的?

However, this cursor comes back as having a length of 0. How can this be? Why is the ID incorrect?

推荐答案

我在本地尝试和它的作品对我来说,这是我的code稍微从你的改编:

I tried locally and it works for me, here's my code slightly adapted from yours:

public void testContacts(final @Nonnull Context context, final int rawContactId, final @Nonnull String expectedDisplayName) {
    Uri entityUri = Uri.withAppendedPath(
            ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId),
            ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY);

    Cursor resultData = context.getContentResolver().query(
            entityUri,
            new String[]{
                    ContactsContract.RawContacts.SOURCE_ID,
                    ContactsContract.RawContacts.Entity.DATA_ID,
                    ContactsContract.RawContacts.Entity.MIMETYPE,
                    ContactsContract.RawContacts.Entity.DATA1
            },
            null, null, null);

    int displayNameId = -1;
    try {
        final int columnIndexDataId = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA_ID);
        final int columnIndexMimetype = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.MIMETYPE);
        final int columnIndexData = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA1);
        while (resultData.moveToNext()) {
            if (!resultData.isNull(columnIndexDataId)) {
                if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(resultData.getString(columnIndexMimetype)) &&
                        expectedDisplayName.equals(resultData.getString(columnIndexData))) {
                    displayNameId = resultData.getInt(1);
                    break;
                }
            }
        }
    } finally {
        resultData.close();
    }

    String reLookedUpDisplayName = null;
    if (displayNameId != -1) {
        Cursor reLookupCursor = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[] {
                        ContactsContract.Data._ID,
                        ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
                },
                ContactsContract.Data._ID + "=?",
                new String[] {String.valueOf(displayNameId)},
                null);
        try {
            final int columnIndexId = reLookupCursor.getColumnIndex(ContactsContract.Data._ID);
            final int columnIndexDisplayName = reLookupCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
            while (reLookupCursor.moveToNext()) {
                reLookedUpDisplayName = reLookupCursor.getString(columnIndexDisplayName);
            }
        } finally {
            reLookupCursor.close();
        }
    }

    Toast.makeText(
            context,
            reLookedUpDisplayName != null ? "Found re-looked up name: " + reLookedUpDisplayName : "Didn't find name re-looking it up",
            Toast.LENGTH_LONG)
            .show();
}

有从code没有大的区别,所以比较或试图取代它的位来看看你有问题。请确保您使用一个新的光标对于每个查询,并正确地关闭后(在最后条款)。

There's no big difference from your code, so compare or try to replace bits of it to see where you have a problem. Make sure you use a fresh Cursor for each query, and close it correctly afterwards (in a finally clause).

另一件事,确保如果(resultData.getString(2).equals(Fields.DISPLAY_NAME))真的是什么你想要做(把它比进入MIME类型 Fields.DISPLAY_NAME ),但因为你说你得到的数据ID正确这不应该成为问题。

Another thing, make sure that if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) is really what you're wanting to do (it compares the entry mime type with Fields.DISPLAY_NAME), but since you're saying you get the data ID correctly this shouldn't be the problem.

这篇关于由数据ID更新联系人数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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