按姓名搜索联系人

Roh*_*wal 2 android android-contacts android-cursor android-search

我正在尝试创建一个自定义联系人应用程序,它只显示那些具有Contact Number 的联系人。首先,有没有什么自动化的方法来做到这一点?假设不是,那么我正在尝试按名称搜索联系人,例如Rohan

这是代码:-

Cursor photoCursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME },
            ContactsContract.Contacts.DISPLAY_NAME + " = ?",
            new String[]{"Rohan"}, null);
    photoCursor.moveToFirst();
    while (photoCursor.moveToNext()) {
        Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
    }
Run Code Online (Sandbox Code Playgroud)

虽然联系人存在,但我没有收到任何日志,如果我删除Selection & Selection Args,我会在日志中看到Rohan。我究竟做错了什么?

Jos*_*ter 6

搜索部分显示名称的简单解决方案。

ContentResolver contentResolver = getCurrentActivity().getContentResolver();

String   whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + searchText + "%" };

Cursor contactCursor = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        whereString,
        whereParams,
        null );

while( contactCursor.moveToNext() ) {

    int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );

    Log.d( "Contact ID", contactId)

}

contactCursor.close();
Run Code Online (Sandbox Code Playgroud)