0
我必須創建一個列表視圖,其中包含手機上所有聯繫人的姓名和電話號碼。 我的代碼正在工作,但用戶有很多聯繫時花費的時間太多。 你有想法,我可以改進它嗎?改善時間獲取電話號碼
public List<Contact> fetchContacts() {
String phoneNumber = null;
String email = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, DISPLAY_NAME + " ASC");
List<Contact> contactList = new ArrayList<Contact>();
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
phoneCursor.moveToNext();
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
phoneCursor.close();
Contact contacto = new Contact();
contacto.name = name;
contacto.phone = phoneNumber;
contacto.id = contact_id;
contactList.add(contacto);
}
}
}
return contactList;
}
不要把你的問題的標題您的谷歌搜索回收快的觀點把您期望從喲看到的*實際搜索結果您的問題標題中包含Google搜索*。 – 2014-09-22 19:11:28