我想從聯繫人名稱中檢索電話號碼。所以我有這個代碼在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中的名稱完全不匹配,它將不會返回任何內容。因此,即使輸入名稱不太可能,是否有任何方法來檢索電話號碼?
例如,有一個聯繫人:原型Alex和我的輸入名稱是:prototype或alex。
''Prototype Alex'LIKE'%prototype%''是真的(如果這是有效的語法) – zapl 2012-04-16 13:36:38