Kinjal
Kinjal

Reputation: 1193

How to get list of Call log with descending order in android?

How to get list of Call log with descending order in android my code is here but its not work it. it just display call log ascending order.

My code is

    @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);   }

i am using Comparator and Collections.reverseOrder() but its not work it.

Upvotes: 2

Views: 3857

Answers (2)

CHarris
CHarris

Reputation: 2793

"COLUMN_FOR_SORT DESC" didn't work for me.

I used:

Cursor managedCursor1 = getContentResolver().query(
        CallLog.Calls.CONTENT_URI, null, null, null,CallLog.Calls.DATE + " DESC");

which did the job.

Upvotes: 8

Arash GM
Arash GM

Reputation: 10395

The last argument of content provider query will sort data which you are passing null to it .

managedCursor1=getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
            null, null, null);

change it to :

managedCursor1=getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
            null, null, "COLUMN_FOR_SORT DESC");

Upvotes: 1

Related Questions