【问题标题】:how to get the number of the most frequent value entry in callLog.calls?如何获取 callLog.calls 中最频繁的值条目的数量?
【发布时间】:2013-07-21 15:51:48
【问题描述】:
Call Log Calls entry is this

 - Name        Number         TYPE          date called
 - Jed         12345          Incoming      7-18-2013
 - Roger       14611          Incoming      7-18-2013
 - Jed         12345          Incoming      7-18-2013
 - Jed         12345          Incoming      7-18-2013
 - Kevin       11111          Incoming      7-18-2013

嗨,我想在 android 中查询,这样我只会检索 Jed, 12345

    Date date=new Date() ;  

    Cursor c = contxt.getContentResolver().query(CallLog.Calls.CONTENT_URI,
            null, CallLog.Calls.TYPE + " AND " + CallLog.Calls.INCOMING_TYPE + 
            " AND " + CallLog.Calls.Date + ">=" + date.getDate() ,
            null,
            CallLog.Calls.DATE + " DESC LIMIT 1");
    if(c!=null)
        do{
            int callCounter = c.getCount();
            String num = callLog_cursor.getString(callLog_cursor
                .getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        }while(c.moveToFirst());

【问题讨论】:

  • 尝试使用 GROUP BY 和 ORDER BY COUNT(*) DESC。
  • 嗨!我如何在我的查询中应用它?你可以为我重新编写代码吗?我真的不熟悉 orderby 和 group by 子句,谢谢!我真的需要我能得到的所有帮助:)

标签: android sqlite


【解决方案1】:

如果您正在寻找一个查询来完成这项工作,我很遗憾地说它(就我的谷歌技能而言)不存在。我已经用另一种你可能会觉得有用的方式解决了它。

在您的活动中的某处创建此函数:

public static void searchAndDisplay(ArrayList<String> arr) {

    ArrayList<String> list1 = new ArrayList();
    ArrayList<Integer> list2 = new ArrayList();
    for (int i = 0; i < arr.size(); i++) {
        int index = list1.indexOf(arr.get(i));
        if (index != -1) {
            int newCount = list2.get(index) + 1;
            list2.set(index, newCount);
        } else {
            list1.add(arr.get(i));
            list2.add(1);
        }
    }
    for (int i = 0; i < list1.size(); i++) {
        System.out.println("Number " + list1.get(i) + " occurs "
                + list2.get(i) + " times.");

    }
    int maxCount = 0;
    int index = -1;
    for (int i = 0; i < list2.size(); i++) {
        if (maxCount < list2.get(i)) {
            maxCount = list2.get(i);
            index = i;
        }
    }
    System.out.println("Number " + arr.get(index)
            + " has highest occurrence i.e " + maxCount); // here you might want to do something/return the number with the highest occurences. 
}

然后在你想要光标的地方使用这个:

    Date date = new Date();
    ArrayList<String> allnumbers = new ArrayList();
    Cursor c = this.getContentResolver().query(
            CallLog.Calls.CONTENT_URI,
            null,
            CallLog.Calls.TYPE + " AND " + CallLog.Calls.INCOMING_TYPE
                    + " AND " + CallLog.Calls.DATE + ">=" + date.getDate(),
            null, CallLog.Calls.NUMBER);

    allnumbers.clear();
    if (c != null)
        c.moveToFirst();
    for (int i = 0; c.getCount() > i; i++) {

        String number1 = c.getString(0);

        allnumbers.add(number1);
        c.moveToNext();

    }
    searchAndDisplay(allnumbers);

您可能需要仔细检查您收到的号码是否正确。

让我知道进展如何。 :)

【讨论】:

  • 这是评论多于回答,@LordMarty
  • 删除了答案,因为它不起作用。试图弄清楚。
  • 答案还在,点击“删除”并确认删除。
  • 如果我想获得前 3 名最多的输入怎么办?我该怎么做?嘿嘿tnx!我需要检查任何其他可能性,以便我的应用程序尽可能强大:) tnx!
【解决方案2】:
public void getFrequentContact(ArrayList<String> logEntriesArray) {

    ArrayList<String> numArray        = new ArrayList<String>();
    ArrayList<Integer> callCountArray = new ArrayList<Integer>();
    int maxIndex = 0;

    for (int i = 0; i < logEntriesArray.size(); i++) {
        int index = numArray.indexOf(logEntriesArray.get(i));
        if (numArray.contains(logEntriesArray.get(i))) {
            int newCount = callCountArray.get(index) + 1;
            callCountArray.set(index, newCount);
        } else {
            numArray.add(logEntriesArray.get(i));
            callCountArray.add(1);
        }
    }


    for (int i = 0; i < numArray.size(); i++) {
        System.out.println("Number " + numArray.get(i) + " occurs "
                + callCountArray.get(i) + " times.");
    }


   if(callCountArray.size()>0){ 
     int maxValue = Collections.max(callCountArray);

        for(int i=0; i<callCountArray.size();i++){
            if(callCountArray.get(i)==maxValue)
                maxIndex = i;
        }
   }
    contactNumOfFreqCaller = numArray.get(maxIndex);
    Log.wtf(" FREQ NUM ", contactNumOfFreqCaller);

    }

我对你给的代码做了一些修改嘿嘿 实际上,在获取顶级联系人方面存在问题,所以我对其进行了一些修改 在这里:)顺便感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多