高效获取联系人列表

ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();

ContentResolver cr = getContentResolver();
HashMap<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>();
Cursor phone = cr.query(CommonDataKinds.Phone.CONTENT_URI,
new String[] { CommonDataKinds.Phone.CONTACT_ID,
CommonDataKinds.Phone.DISPLAY_NAME,
CommonDataKinds.Phone.NUMBER,
CommonDataKinds.Phone.DATA1
// CommonDataKinds.StructuredPostal.DATA3,
}, null, null, null);
while (phone.moveToNext()) {
String contactId = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));
String displayName = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
String PhoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String address = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.DATA1));

// 以contactId为主键,把同一人的所有电话都存到一起。
ArrayList<String> ad = hashMap.get(contactId);
if (ad == null) {
ad = new ArrayList<String>();
ad.add(displayName);
ad.add(PhoneNumber);
// ad.add(address);

hashMap.put(contactId, ad);
} else {
ad.add(PhoneNumber);
}

 }
phone.close();

ArrayList<String> tmpList;
String tmpStr = "";
int k;
Iterator iter = hashMap.entrySet().iterator();
while (iter.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();6
tmpList = (ArrayList) val;
 tmpStr = "";
for (k = 1; k < tmpList.size(); k++) {
tmpStr = tmpStr + tmpList.get(k) + ',';
}
tmpStr = GetString(tmpStr);
HashMap<String, Object> tmpMap = new HashMap<String, Object>();
 tmpMap.put("name", tmpList.get(0));
 tmpMap.put("number", tmpStr);
 tmpMap.put("imgView", R.drawable.ic_launcher);
items.add(tmpMap);
 }
 return items;
}

private String GetString(String str) {

String strLast = "";
 int i = str.lastIndexOf(",");
 if (i > 0) {
strLast = str.substring(0, str.length() - 1);
 }
 return strLast.replace(" ", "").replace(",", "\n").replace("+86", "");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值