連絡先の名前から電話番号を取得したい。私は、このコードSQLiteの文字列でのクエリ
public String getPhoneNumber(String name) {
String phonenumber = "";
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME LIKE '%" + name + "%'", null, null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
//phonenumber = number;
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
phonenumber = number;
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
//phonenumber = number;
// do something with the Work number here...
break;
}
}
phones.close();
//
// Get all email addresses.
//
}
cursor.close();
return phonenumber;
}
を持っていますが、連絡先の名前は、DB内の名前と正確に一致していない場合、それは何も返さないでしょう。だから、たとえ入力の名前でさえも電話番号を取得する方法がないのであれば、それはあまりありませんか?
たとえば、連絡先があります:プロトタイプアレックスと私の入力名は:プロトタイプまたはアレックスです。
'Prototype Alex 'LIKE'%prototype% ''は本当です(それが有効な構文だった場合) – zapl