栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过电话簿在我的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.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440837.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号