如何计算arraylist中的重复联系人数量

时间:2014-04-01 06:16:53

标签: android arraylist

我有一个Arraylist,其中包含来自通话记录的Number,Name。要删除此arraylist中的副本,我使用的方法首先检查arraylist中的数字而不是返回结果。
 我的代码是

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();
}

所有这些工作正常现在我必须计算每个联系人。以便我可以通过大多数联系的联系人安排我的列表。我尝试了很多,但它给了我错误的输出。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个,

 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保留列表的顺序。 refer here了解更多详情