我有功能加载android中的所有联系号码,现在我想过滤所有联系号码,前缀为“0878”,“0877”,“0817”,因此将加载带有该前缀的联系号码,这里是我当前的代码:
private void loadContact(){
ContentResolver cr = applicationContext.getContentResolver();
String [] PROJECTION = ContactsQuery.PROJECTION;
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
null
);
while (cursor.moveToNext()) {
//String name = cursor.getString(ContactsQuery.DISPLAY_NAME);
String lookUpKey = cursor.getString(ContactsQuery.LOOKUP_KEY);
loadContactDetail(lookUpKey);
}
cursor.close();
}
private void loadContactDetail(String lookUpKey){
ContentResolver cr = applicationContext.getContentResolver();
String [] COLS = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, COLS,
ContactsContract.Data.LOOKUP_KEY + " = ?",
new String[]{lookUpKey},
null
);
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatted = Util.formatMSISDN(phone);
if(formatted != null)
contactsSet.add(formatted);
}
cursor.close();
}
答案 0 :(得分:2)
试试这个。
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatted = Util.formatMSISDN(phone);
if(formatted != null){
if(formatted.startsWith("0878") || formatted.startsWith("0877") || formatted.startsWith("0817"))
contactsSet.add(formatted);
}
}
cursor.close();
答案 1 :(得分:0)
查看sourceString.contains(CharSequence)方法。