我想按日期将通话记录和收件箱消息中的电话号码显示在一个listView
中。我正在使用两个光标;一个检索呼叫,第二个检索传入的消息。其中两个按日期排序。但是首先我填充联系人,然后填充消息。
如何用按日期排序的联系人和消息填充此列表?
这是我的代码:
public static List<Call> getCallHistory() {
boolean filter = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE).getInt("filter", 0) == 1;
String[] strFields = { CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE, CallLog.Calls.DATE,
CallLog.Calls.DURATION };
String strOrder = CallLog.Calls.DATE + " DESC";
final Cursor cursorCall = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, strFields, null, null, strOrder);
List<Call> callList = new ArrayList<Call>();
while (cursorCall.moveToNext()) {
Call call = new Call();
call.setNumber(cursorCall.getString(cursorCall.getColumnIndex(CallLog.Calls.NUMBER)));
if (cursorCall.getInt(cursorCall.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.OUTGOING_TYPE) {
call.setOut(true);
} else {
call.setOut(false);
}
call.setName(cursorCall.getString(cursorCall.getColumnIndex(CallLog.Calls.CACHED_NAME)));
call.setTime(cursorCall.getLong(cursorCall.getColumnIndex(CallLog.Calls.DATE)));
call.setDuration(cursorCall.getLong(cursorCall.getColumnIndex(CallLog.Calls.DURATION)));
if (filter && cursorCall.getString(cursorCall.getColumnIndex(CallLog.Calls.CACHED_NAME)) != null) {
continue;
}
callList.add(call);
}
cursorCall.close();
return getMessageHistory(callList);
// return callList;
}
public static List<Call> getMessageHistory(List<Call> callList) {
String[] projection = { "address", "date", "body" };
String sortOrder = "date DESC";
Cursor cursor = context.getContentResolver().query(SMS_INBOX_CONTENT_URI, projection, null, null, sortOrder);
while (cursor.moveToNext()) {
Call call = new Call();
call.setNumber(cursor.getString(cursor.getColumnIndex("address")));
call.setTime(cursor.getLong(cursor.getColumnIndex("date")));
call.setBody(cursor.getString(cursor.getColumnIndex("body")));
call.setSms(true);
callList.add(call);
}
return callList;
}
最佳答案
您应该使用Android MergeCursor,它是专门为此目的而创建的。
在你的情况下
Cursor cursorCall = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, strFields, null, null, strOrder);
Cursor cursorInbox = context.getContentResolver().query(SMS_INBOX_CONTENT_URI, projection, null, null, sortOrder);
Cursor merged = new MergeCursor(new Cursor[] { cursorCall, cursorInbox });
如果不匹配的列导致问题,则可以使用CursorWrapper对齐它们。