我的测试电话中有100个联系人,并尝试使用此代码进行搜索:
MatrixCursor cursor = new MatrixCursor(new String[] {"type", "_id", "detail", "optional_data", "lob_data"});
Cursor c;
String selection;
String search = selectionArgs[0].toLowerCase();
try {
selection = "("+ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?) collate nocase";
c = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.PHOTO_URI},
selection,
new String[]{"%"+search+"%"},
null);
c.moveToFirst();
c.moveToNext();
while (!c.isAfterLast()) {
Object[] rowObject = new Object[] {"Contact", c.getString(0), c.getString(1), null, imageToByte(c.getString(2))};
cursor.addRow(rowObject);
c.moveToNext();
}
}
catch (Exception e){
Log.e(TAG, "URI problem with Contacts: "+e);
}
如果我搜索它不会返回所有结果。例如,如果我搜索 '和' , 'der' 或 'all' ,但如果我搜索 'ale' , 'lex' , 'xan' 或 'hal' 。如果在任何搜索中字母超过四,我根本得不到任何答案,如果有人能够向我解释这个逻辑,我会非常高兴。
我还想补充一下,我从不同排序参数的搜索得到不同的结果,如果我进行空搜索,我会得到所有人,所以搜索结果的最大数量似乎不是问题。
任何帮助都非常有用,谢谢。
答案 0 :(得分:0)
你应该尝试为游标设置迭代器
try {
while (cursor.moveToNext()) {
...
}
} finally {
cursor.close();
}
MatrixCursor cursor = new MatrixCursor(new String[] {"type", "_id", "detail", "optional_data", "lob_data"});
Cursor c;
String selection;
String search = selectionArgs[0].toLowerCase();
try {
selection = "("+ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?) collate nocase";
c = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.PHOTO_URI},
selection,
new String[]{"%"+search+"%"},
null);
c.moveToFirst();
//c.moveToNext();
while (c.moveToNext()) {
Object[] rowObject = new Object[] {"Contact", c.getString(0), c.getString(1), null, imageToByte(c.getString(2))};
cursor.addRow(rowObject);
//c.moveToNext();
}
}
catch (Exception e){
Log.e(TAG, "URI problem with Contacts: "+e);
}