如何让ArrayList中重复的联系人数量 [英] how to get count of duplicate contacts in arraylist
本文介绍了如何让ArrayList中重复的联系人数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含从通话记录编号,名称的ArrayList。从此ArrayList我用哪个先检查ArrayList中的数量比返回结果的方法删除重复的。结果
我的code是
I have an Arraylist which contains Number,Name from call log. to remove the duplicate from this arraylist I used a method Which first check the number in the arraylist than return the result.
My code is
public void getnumber(ContentResolver cr) {
aa.clear();
num.clear();
String viaOrder = android.provider.CallLog.Calls.DATE + " DESC," + android.provider.CallLog.Calls.NUMBER + " DESC";
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null,
null,
viaOrder);
while (c.moveToNext()) {
Info info = new Info();
info.phone=c.getString(c.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
if(num.contains(info.phone)){
count++;
//int cnt = aa.indexOf(count);
//aa.set(cnt, count);
}
else {
num.add(info.phone);
String nam =c.getString(c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
if(nam==null){
nam = "Unknown";
}
nam+=count;
byte[] imagearray = null;
aa.add(new ContactStock(nam,info.phone,imagearray,count));
count=1;
}
}
c.close();
}
这一切工作正常,现在我得每个联系人的数量。这样我可以安排通过最联络的联系人名单。我尝试了很多,但它给了我错误的输出。任何帮助将AP preciated。
All this working fine Now I have to get count of each contact. so that I can arrange my list by most contacted contacts. I tried it a lot but it gives me wrong output. Any help Will be appreciated.
推荐答案
试试这个,
public void getnumber(ContentResolver cr) {
aa.clear();
num.clear();
String viaOrder = android.provider.CallLog.Calls.DATE + " DESC," + android.provider.CallLog.Calls.NUMBER + " DESC";
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null,
null,
viaOrder);
while (c.moveToNext()) {
Info info = new Info();
info.phone=c.getString(c.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
info.name = c.getString(c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
num.add(info.toString());
}
c.close();
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (String temp : num) {
Integer count = map.get(temp);
map.put(temp, (count == null) ? 1 : count + 1);
}
printMap(map);
//System.out.println("\nSorted Map");
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
printMap(treeMap);
}
public static void printMap(Map<String, Integer> map){
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
aa.add(new ContactStock("Name "+entry.getValue(),entry.getKey() , imagearray, entry.getValue()));
}
}
LinkedHashset保留O对你的列表中的顺序。 这里指了解更多详情
这篇关于如何让ArrayList中重复的联系人数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文