ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Contacts.CONTENT_URI, new String[] {
Contacts._ID
}, null, null, Contacts._ID + " ASC");
int contactCount = cursor.getCount();
if (contactCount > 0) {
StringBuilder idList = new StringBuilder();
while (cursor.moveToNext()) {
idList.append(cursor.getInt(0));
if (!cursor.isLast()) {
idList.append(", ");
} else {
idList.append(")");
}
}
String where = Phone.CONTACT_ID + " in (" + idList.toString();
Cursor numberCursor = resolver.query(Phone.CONTENT_URI, new String[] {
Phone._ID, Phone.CONTACT_ID, Phone.NUMBER
}, where, null, Phone.CONTACT_ID + " ASC");
while (numberCursor.moveToNext()) {
...
}
}
cursor.close();
100% 완성한 코드는 아닙니다.
느린 이유는 불필요하게 while 문 안에서 또 while 을 돌아야 하는 것 때문인데요.
위에 고친것처럼 독립적으로 while 문을 돌면 좀 덜 느릴꺼에요. 그래도 느리긴 하겠지만.
SQL query 문에서 where 절에는 in 을 사용해서 한번에 검색 해보세요.