访问 Android 联系人所有详细信息的最佳方式

发布于 2024-12-21 05:18:55 字数 162 浏览 2 评论 0原文

我正在编写一个应用程序来与我的服务器同步联系人详细信息。为此,我需要多次使用内容提供程序查询联系人,因为所有数据并不存在于一张表中。例如,通过使用联系人 ID,我必须分别查询每个联系人的电话、电子邮件和地址表,我觉得这效率不高。如果有人能指出我在单个查询中获取所有联系方式的方法,那将非常有帮助。提前致谢 :)

I am writing an application to sync contact details with my server. For this I need to query Contacts using content provider multiple times as all the data is not present in one table. For example by using contact id, I have to query separately to phone, email and address tables for each contact which I feel is not much efficient. It would be really helpful if someone could point me out ways to get all the contact details in a single query. Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ι不睡觉的鱼゛ 2024-12-28 05:18:55

如果您有 rawContactId,则不需要多次查询。您可以使用 Data.CONTENT_URI 作为 uri 进行单个查询,并在 rawContactId 上进行选择。

您必须使用结果光标循环才能读取信息。要知道在哪一列中,您需要查看数据表中的给定行,您需要检查 MIMETYPE

编辑

private interface DataQuery {
    public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, };

    public static final int COLUMN_ID = 0;
    public static final int COLUMN_MIMETYPE = 1;
    public static final int COLUMN_DATA1 = 2;
    public static final int COLUMN_DATA2 = 3;
    public static final int COLUMN_DATA3 = 4;
    public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
    public static final int COLUMN_PHONE_TYPE = COLUMN_DATA2;
    public static final int COLUMN_GIVEN_NAME = COLUMN_DATA2;
    public static final int COLUMN_FAMILY_NAME = COLUMN_DATA3;

    public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";
}


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

try {
    while (c.moveToNext()) {
        final long id = c.getLong(DataQuery.COLUMN_ID);
        final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
        uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);

        if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
            final String oldLastName = c.getString(DataQuery.COLUMN_FAMILY_NAME);
            final String oldFirstName = c.getString(DataQuery.COLUMN_GIVEN_NAME);
            //store them where you need
        } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
            final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);
            final String cellPhone = c.getString(DataQuery.COLUMN_PHONE_NUMBEIR);
            //store them where you need
            }
        }
    } // while
} finally {
    if (c!=null) c.close();
}

请考虑我没有检查代码:我这里没有编译器。无论如何我希望它会有用

If you have the rawContactId you don't need multiple query. You can have a single query with Data.CONTENT_URI as uri and with a selection on your rawContactId.

The you have to loop with the resulting cursor to read the info. To know in wich column you need to see for a given row in the Data table you need to check the MIMETYPE

EDIT

private interface DataQuery {
    public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, };

    public static final int COLUMN_ID = 0;
    public static final int COLUMN_MIMETYPE = 1;
    public static final int COLUMN_DATA1 = 2;
    public static final int COLUMN_DATA2 = 3;
    public static final int COLUMN_DATA3 = 4;
    public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
    public static final int COLUMN_PHONE_TYPE = COLUMN_DATA2;
    public static final int COLUMN_GIVEN_NAME = COLUMN_DATA2;
    public static final int COLUMN_FAMILY_NAME = COLUMN_DATA3;

    public static final String SELECTION = Data.RAW_CONTACT_ID + "=?";
}


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

try {
    while (c.moveToNext()) {
        final long id = c.getLong(DataQuery.COLUMN_ID);
        final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
        uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);

        if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
            final String oldLastName = c.getString(DataQuery.COLUMN_FAMILY_NAME);
            final String oldFirstName = c.getString(DataQuery.COLUMN_GIVEN_NAME);
            //store them where you need
        } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
            final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);
            final String cellPhone = c.getString(DataQuery.COLUMN_PHONE_NUMBEIR);
            //store them where you need
            }
        }
    } // while
} finally {
    if (c!=null) c.close();
}

Please consider that I did not check the code: I do not have a compiler here. I hope it will be useful anyway

五里雾 2024-12-28 05:18:55

Data 是一个通用表,可以容纳任何类型的联系人数据

给定行中存储的数据类型由该行的 MIMETYPE 值指定,该值确定通用列 DATA1 到 DATA15 的含义。

例如,如果数据类型为 Phone.CONTENT_ITEM_TYPE,则列 DATA1 存储电话号码,但如果数据类型为 Email.CONTENT_ITEM_TYPE,则 DATA1 存储电子邮件地址。同步适配器和应用程序可以引入自己的数据类型。

Data is a generic table that can hold any kind of contact data.

The kind of data stored in a given row is specified by the row's MIMETYPE value, which determines the meaning of the generic columns DATA1 through DATA15.

For example, if the data kind is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address. Sync adapters and applications can introduce their own data kinds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文