0

在 android 中检索呼叫日志时,当我尝试根据索引 1 的投影在“\n”上拆分结果时,索引超出范围

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

不确定我是否在这里遗漏了什么。我真的很感激任何帮助。

吐司连续显示 3 个值

谢谢

4

1 回答 1

0

感谢@Martin Foot,我找到了答案

for 循环实际上仅获得单个结果,而不是所有结果。因此,当获取 callLogCusor.getString(i) 时,我得到的是特定列的值,而不是所有行。

代码现在看起来像

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

            }
         }
于 2012-12-25T12:53:57.070 回答