通过电话簿在我的App中多次显示一些联系人

通过电话簿在我的App中多次显示一些联系人,第1张

通过电话簿在我的App中多次显示一些联系人

您正在为每个电话的每个联系人打印那些“
FetchContacts”日志,因此,如果一个联系人为她存储了多个电话,您将看到它多次打印(即使它是相同的电话号码)。

如果您安装了类似Whatsapp的应用程序,那么几乎总是会看到每个联系人的所有电话号码重复,从而导致这些日志的打印次数超过每个联系人一次。

而且,这是通过电话获得联系的缓慢而痛苦的方式。取而代之的是,您可以直接通过Phones.CONTENT_URI直接查询并获取数据库中的所有电话,然后通过Contact-
ID将它们映射到联系人中:

Map<String, List<String>> contacts = new HashMap<String, List<String>>();String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null);while (cur != null && cur.moveTonext()) {    long id = cur.getLong(0); // contact ID    String name = cur.getString(1); // contact name    String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234    Log.d(TAG, "got " + id + ", " + name + ", " + data);    // add info to existing list if this contact-id was already found, or create a new list in case it's new    String key = id + " - " + name;    List<String> infos;    if (contacts.containsKey(key)) {        infos = contacts.get(key);    } else {        infos = new ArrayList<String>();        contacts.put(key, infos);    }    infos.add(data);}// contacts will now contain a mapping from id+name to a list of phones.// you can enforce uniqueness of phones while adding them to the list as well.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5142105.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存