How do I get the number of the most frequently entered value 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 so i will only get Jed 12345 <since it has the most duplicate value in the list im suppose to do it in sqlite (android query) but i dont know which functions to call This is the code which I used, but I was only able to get the last number that is called, not the one with the most entries. HOW DO I DESTROY THE QUESTION?
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());
source to share
If you're looking for a single request to complete an assignment, I'm sorry to say that it (didn't get that far from my google skills) doesn't exist. I solved this in a different way, which you may find helpful.
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 wherever you want to use the cursor 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 may want to double check that the received numbers are correct.
Let me know how it goes. :)
source to share
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);
}
i revised a bit the code you gave hehe actually had a problem getting the top pins so i revisited a bit here it goes :) thanks by the way for the help !!
source to share