如何在android我的代码中获取具有降序的调用日志列表,但它不起作用。它只显示通话记录升序。
我的代码是
@Override
protected List<CallLogDetail> doInBackground(Void... params) {
data = new ArrayList<CallLogDetail>();
managedCursor1=getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, null);
int number = managedCursor1.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor1.getColumnIndex(CallLog.Calls.TYPE);
int Name= managedCursor1.getColumnIndex(CallLog.Calls.CACHED_NAME);
while (managedCursor1.moveToNext()) {
String phNumber = managedCursor1.getString(number);
String Names = managedCursor1.getString(Name);
Bitmap photo = null
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(phNumber));
String[] projection = new String[]{ContactsContract.Contacts._ID};
Cursor c = getActivity().getContentResolver().query(contactUri, projection,
null, null, null);
if (c.moveToFirst()) {
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
}
c.close();
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
String callTypeCode = managedCursor1.getString(type);
int callcode = Integer.parseInt(callTypeCode);
String callType = null;
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
CallLogDetail c = new CallLogDetail(Names, phNumber, photo, callType);
data.add(c);
Comparator<CallLogDetail> callLogDetailComparator = Collections.reverseOrder();
Collections.sort(data, callLogDetailComparator); }
我正在使用比较器和 Collections.reverseOrder(),但它无效。
答案 0 :(得分:5)
&#34; COLUMN_FOR_SORT DESC&#34;没有为我工作。
我用过:
Cursor managedCursor1 = getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null,CallLog.Calls.DATE + " DESC");
完成了这项工作。
答案 1 :(得分:1)
内容提供程序查询的最后一个参数将对要传递null的数据进行排序。
managedCursor1=getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, null);
将其更改为:
managedCursor1=getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, "COLUMN_FOR_SORT DESC");