あなたのコードを修正しました, 残りを処理することを確認してください 私は例を作りました これは修正されたコードです:
String name = "contact_name_to_search_for"
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,"DISPLAY_NAME" + " LIKE '%"+name+"%'", null,
ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED + " DESC");
これは、たとえば私が作成した完全に処理されたコードです
String name = "contact_name_to_search_for"
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,"DISPLAY_NAME" + " LIKE '%"+name+"%'", null,
ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED + " DESC");
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)).toLowerCase();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId.toLowerCase(), null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Toast.makeText(getApplicationContext(), number+" Home number",Toast.LENGTH_SHORT ).show();
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Toast.makeText(getApplicationContext(), number+" Mobile number",Toast.LENGTH_SHORT ).show();
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Toast.makeText(getApplicationContext(), number+" Work number",Toast.LENGTH_SHORT ).show();
break;
}
}
phones.close();
}
cursor.close();