Getting call log in android returning index out of bound when splitting result row on "\n"

StackOverflow https://stackoverflow.com/questions/14030028

  •  12-12-2021
  •  | 
  •  

Question

while retrieving call log in android, I am getting an index out of bound when trying to split the result on "\n" based on the projection for index 1

    /* 
     * projection string that will contain the values retrieved 
     */
    String[] projection = new String[] { Calls.DATE, Calls.NUMBER, Calls.DURATION };

    /*
     * Cursor to loop on the results of the query
     */
    Cursor callLogCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER); 

    callLogCursor.moveToFirst();

    /*
     * Loop through the call log and get the number needed or until 
     * we retrieved all call logs
     */ 
    String row;
    while (callLogCursor.isAfterLast() == false) {
        for (int i=0; i<callLogCursor.getColumnCount(); i++) {
            row = callLogCursor.getString(i);
            String[] parts = row.split("\n");
            showToast(callLogCursor.getString(i));
            callLogs += CommonUtils.formatDate(parts[0]) + "-" + parts[1] + "-" + parts[2] + "\n";
         }
          numberOfCallLogRetrieved++;
          callLogCursor.moveToNext();
    }

Not sure if I am missing something here. I really appreciate any help.

The toast is showing 3 values consecutively

Thanks

Was it helpful?

Solution

I found the answer thanks to @Martin Foot

the for loop actually gets individual result alone not all results. so when getting the callLogCusor.getString(i), I am getting the value of a specific column, not all the row.

the code now looks like

for (int i=0; i<callLogCursor.getColumnCount(); i++) {
            if(i == 0){
                String type = getLogType(callLogCursor.getString(i));
                callLogs += type;
            } else if (i == 1){
                callLogs += "-" + CommonUtils.formatDate(callLogCursor.getLong(i));
            } else if (i == 2){
                callLogs += "-" + callLogCursor.getString(i);
            } else if (i == 3){
                callLogs += "-" + CommonUtils.formatDuration(callLogCursor.getInt(i)) + "\n";

            }
         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top