Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

android - how to get the number of the most frequent value entry in callLog.calls?

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

Hi, i want to query in android such that i will only retrieve Jed, 12345 << since he has the most repetitive value in the list, im suppose to do this in sqlite (android query) but i dont know which functions to invoke This is the code i used, but i was only able to get the recent number that called instead of the one with the most entries. HOW DO I DO THE QUERY?

    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());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're looking for a single query to do the job, I'm sorry to say that it (as far sa my google skills goes) doesn't exist. I've solved it in another way that you might find useful.

Create this function somewhere in your activity:

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. 
}

Then where you want the cursor you use this:

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

You might want to double-check that the numbers you receive is correct.

Let me know how it goes. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...