我想在按日期排序的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;
}
答案 0 :(得分:2)
您应该使用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对齐它们。