Beni
Beni

Reputation: 149

Cursor query order by Date

I have a TextView to which I'm putting my contacts which have birthdays:

private String loadContacts()
{
    StringBuilder builder = new StringBuilder();
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null,
            null, null);

    if (cursor.getCount() > 0){
        while (cursor.moveToNext()){
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            String columns[] = {
                    ContactsContract.CommonDataKinds.Event.START_DATE,
                    ContactsContract.CommonDataKinds.Event.TYPE,
                    ContactsContract.CommonDataKinds.Event.MIMETYPE,
            };

            String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
                    ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
                    " and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
                    " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE +
                    "' and " + ContactsContract.Data.CONTACT_ID + " = " + id;

            String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";

            Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
                    null, sortOrder);

            if (birthdayCur.getCount() > 0) {
                while (birthdayCur.moveToNext()) {
                    String birthday = birthdayCur.getString(birthdayCur.
                            getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));

                    builder.append("Contact: ").append(name).append(" ").append(birthday).append("\n\n");
                }
            }
            birthdayCur.close();
        }
    }
    cursor.close();

    return builder.toString();
}

And then assign the contacts on the "onCreate" function

  if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_CONTACTS }, 1);
    } else {
        listContacts.setText(loadContacts());
    }

This line of code:

String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
                    null, sortOrder);

doesn't seem to work no matter what I put there to order it (name, _ID etc.).

I am trying to sort by Birthdays but even if I try to sort by name it doesn't work. Why ?

Am I using wrong type of View(TextView) or is it the way I use StringBuilder to set the text or something else entirely?

My minSdkVersion is 19 and targetSdkVersion 26. I'm testing this App on my Nexus5(api 23 android 6.01) on debugging mode.

I appreciate any advice.

Upvotes: 0

Views: 1818

Answers (1)

Beni
Beni

Reputation: 149

Well this is what happens when I hurried. It was actually beyond simple. I just needed to put all my needed columns in the first Cursor and the second more important thing was to use "ContactsContract.Data.CONTENT_URI" instead of "ContactsContract.Contacts.CONTENT_URI" because the "DATA.CONTENT_URI" actually has all of my needed columns and "Contacts.CONTENT_URI" doesn't.

As simple as that.

private String loadContacts()
    {
        StringBuilder builder = new StringBuilder();
        ContentResolver cr = getContentResolver();

        String columns[] = {
                ContactsContract.CommonDataKinds.Event.START_DATE,
                ContactsContract.Contacts.DISPLAY_NAME
        };

        String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
                ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
                " and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
                " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "'";

        String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";

        Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
                columns, where,
                null, sortOrder);

        if (cursor.getCount() > 0){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String birthday = cursor.getString(cursor.
                        getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));

                builder.append("Contact: ").append(name).append(birthday).append(" ").append("\n\n");
            }
        }
        cursor.close();

        return builder.toString();
    }

Upvotes: 2

Related Questions