本文介绍了如何在一个 SQLite 查询中获取联系人 ID、电子邮件、电话号码?联系人安卓优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想至少使用一个电话号码获取所有联系人,还需要每个联系人的所有电话号码和所有电子邮件.

I want to fetch All Contacts atleast with one phone Number, also I want all Phone Numbers and All emails for every Contact.

当前代码:

// To get All Contacts having atleast one phone number.

Uri uri = ContactsContract.Contacts.CONTENT_URI;
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " > ?";
String[] selectionArgs = new String[] {"0"};
Cursor cu = applicationContext.getContentResolver().query(uri,
                null, selection, selectionArgs, null);

// For getting All Phone Numbers and Emails further queries :
while(cu.moveToNext()){
String id = cu.getString(cu.getColumnIndex(ContactsContract.Contacts._ID));


 // To get Phone Numbers of Contact
    Cursor pCur = context.getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
 new String[]{id}, null);

// To get Email ids of Contact
Cursor emailCur = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);

// Iterate through these cursors to get Phone numbers and Emails
}

如果我的设备中的联系人超过 1000 个,则花费的时间太长.如何在单个查询中获取所有数据,而不是为每个联系人做两个额外的查询?

If there are more than 1000 contacts in my Device, it is taking too much time. How can I get All Data in single query, rather than doing two additional queries for each contact?

或者还有其他优化方法吗?

Or is there any other way to optimize?

提前致谢.

推荐答案

ICS:当您从 Data.CONTENT_URI 查询时,您已经拥有关联 Contact 中的所有行加入 - 即这会起作用:

ICS: When you query from Data.CONTENT_URI you have all the rows from the associated Contact already joined - i.e. this would work:

ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(
        Data.CONTENT_URI,
        null,
        Data.HAS_PHONE_NUMBER + "!=0 AND (" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?)",
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
        Data.CONTACT_ID);

while (c.moveToNext()) {
    long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID));
    String name = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));
    String data1 = c.getString(c.getColumnIndex(Data.DATA1));

    System.out.println(id + ", name=" + name + ", data1=" + data1);
}

如果您的目标是 2.3,您需要考虑到 HAS_PHONE_NUMBER 无法通过查询 Data 时使用的连接获得的事实.

If you are targeting 2.3 you need to account for the fact that HAS_PHONE_NUMBER is not available through the joins used when querying Data.

有趣.

例如,这可以通过跳过您要求联系人必须有电话号码的要求而解决,而是满足于至少有电话号码或电子邮件地址的任何联系人:

This could, for instance, be solved either by skipping your requirement that the contact must have a phone number and instead settle for "any contact with at least a phone number or an e-mail address":

Cursor c = resolver.query(
        Data.CONTENT_URI,
        null,
        Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?",
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
        Data.CONTACT_ID);

如果这不是一个选项,你总是可以选择一个可怕的hacky子选择:

If that is not an option you can always go for a horribly hacky sub-select:

Cursor c = resolver.query(
        Data.CONTENT_URI,
        null,
        "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) AND " +
        Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts WHERE " + Contacts.HAS_PHONE_NUMBER + "!=0)",
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE}, Data.CONTACT_ID);

或者用two Cursors:

Cursor contacts = resolver.query(Contacts.CONTENT_URI,
        null, Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID + " ASC");
Cursor data = resolver.query(Data.CONTENT_URI, null,
        Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?",
        new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
        Data.CONTACT_ID + " ASC");

int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
boolean hasData = data.moveToNext();

while (contacts.moveToNext()) {
    long id = contacts.getLong(idIndex);
    System.out.println("Contact(" + id + "): " + contacts.getString(nameIndex));
    if (hasData) {
        long cid = data.getLong(cidIndex);
        while (cid <= id && hasData) {
            if (cid == id) {
                System.out.println("	(" + cid + "/" + id + ").data1:" +
                        data.getString(data1Index));
            }
            hasData = data.moveToNext();
            if (hasData) {
                cid = data.getLong(cidIndex);
            }
        }
    }
}

这篇关于如何在一个 SQLite 查询中获取联系人 ID、电子邮件、电话号码?联系人安卓优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:55