Reputation: 2134
Hello i am using the next code for retrieving phone contacts name,number and its taked to long. After littel search i found the the cursor.moveToFirst() command is that one who cuz this . But i couldent find any solution ): may some one help me please ? Thanks !
public void loadPhoneMembers(){
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName =
pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if(!phonesAdded.contains(contactNumber)){
members.add(contactName+","+contactNumber); // members is arrayList
}
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
}
Upvotes: 0
Views: 813
Reputation: 334
You are querying one extra Cursor for each Contact from your first query. You can query the Names and TelephoneNumbers in one query and ignore the Contacts without Telephone number.
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER ,ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection, null, null, null);
Also, while (cursor.moveToNext()){
}
has same effect as the following.
if(cursor.moveToFirst())
{
do
{
} while (cursor.moveToNext()) ;
Upvotes: 4