访问Android通讯录中的所有细节的最佳方式 [英] Best way to access all the details of android contacts

查看:185
本文介绍了访问Android通讯录中的所有细节的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序同步与我的服务器详细联系方式。为此,我需要查询使用内容提供商多次联系,因为所有数据不是在一个表present。例如通过接触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 :)

推荐答案

如果你有rawContactId你不需要多个查询。你可以有Data.CONTENT_URI一个查询的URI,并与你rawContactId选择。

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.

的,你必须用所得光标读取信息循环。要知道在至极柱,你需要看到一个给定的行中,你需要检查MIMETYPE数据表

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

修改

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

请考虑,我没有检查code:我没有编译器在这里。我希望这将是有益的反正

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

这篇关于访问Android通讯录中的所有细节的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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