构建连接蓝牙的设备并检索与某个“名称”匹配的联系人姓名/号码,例如,如果我搜索“A - ”并且我的手机中有以下联系人:
var fnc = new Function("param1", "param2", "alert(param1+param2);");
返回以下列表:
[0] "A - Dad "
[1] "A - Mom"
[2] "A - Grandmother"
[3] "FirendA "
因此,此函数应在匹配的所有联系人数组中返回一个包含联系人姓名和联系号码(仅以+336或+337(移动)开头)的数组。 但到目前为止,我尝试的代码返回给我一个返回空指针异常:
[0]
[0] A - Dad
[1] +33600000000
[1]
[0] A - Mom
[1] +33611111111
[2]
[0] A - Grandmother
[1] +33722222222
我像这样检索数组:
public List<List<String>> getContacts(String name) {
List<List<String>> out = new ArrayList<>();
Cursor cursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID },
ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
ContactsContract.Contacts.DISPLAY_NAME);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String currentName = cursor.getString(1);
String currentId = cursor.getString(2);
Cursor cursor2 = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { currentId }, null);
while (cursor2.moveToNext()) {
String currentNumber = cursor2.getString(0);
if(isMobilePhone(currentNumber)) {
List<String> tmp = new ArrayList<String>();
tmp.add(currentName);
tmp.add(currentNumber);
out.add(tmp);
break;
}
}
}
return out;
}
我做错了什么?
答案 0 :(得分:0)
您的代码过于复杂。
Phones
表SQLITE
的{{1}}运算符在LIKE
字段中搜索您的关键字DISPLAY_NAME
,而不是尺寸为2的Array
此处的代码:
Pair