按日期的游标查询顺序

时间:2017-11-23 10:20:53

标签: android sql api android-cursor

我有一个TextView,我将我的联系人带到了生日:

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

然后在“onCreate”功能上分配联系人

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

这行代码:

String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
                    null, sortOrder);
无论我在那里订购它(名称,_ID等),

似乎都不起作用。

我正在尝试按生日排序,但即使我尝试按名称排序也不起作用。为什么?

我使用的是错误的View(TextView)类型,还是我使用StringBuilder完全设置文本或其他内容的方式?

我的minSdkVersion是19和targetSdkVersion 26.我正在调试模式下在我的Nexus5(api 23 android 6.01)上测试这个应用程序。

我感谢任何建议。

1 个答案:

答案 0 :(得分:0)

这就是我匆忙时会发生的事情。实际上这简直太简单了。 我只需要将所有需要的列放在第一个Cursor中,第二个更重要的是使用“ContactsContract.Data.CONTENT_URI” 而不是“ContactsContract.Contacts.CONTENT_URI”,因为“DATA.CONTENT_URI”实际上具有我所有需要的列,而“Contacts.CONTENT_URI”则没有。

就这么简单。

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