Application error while fetching contact list due to big data

I am trying to get all the phone numbers and names of a contact list. It works great with a small number of contact lists. But the app crashes when getting a lot of contacts. Here is my code:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

int position=0;
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {

        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer.parseInt(cur.getString(cur.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);
            String phone=null;
            while (pCur.moveToNext()) {
                phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }

            SearchGetterSetter searchContactList=new SearchGetterSetter();
            searchContactList.setPosition(position);
            searchContactList.setFist_name(name);
            searchContactList.setContactNo(phone);
            searchContactList.setChecked(false);
            contactList.add(searchContactList);
            originalList.add(searchContactList);
            position++;
        }
    }
    cur.close();
}

      

Is there a way to get a list of contacts in packages?

+3


source to share


1 answer


Large ones handled this way should not run on the UI thread. Java stream. Or better option is using AsyncTask in android API.

private class myAsyncTask extends AsyncTask<(inputParams),(returnTypeForProgressUpdate), (retunType)> {
 protected Long doInBackground(inputParams... inputparam) {
     int input = inputparam[0];
     // Your Code In Here
     return modifiedInput;
 }

 protected void onProgressUpdate(Integer... progress) {
            // for update bar 
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
  //this is in the ui thread so use the result   
  showDialog("Loaded");
 }

      



For more details visit http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground (Params ...)

0


source







All Articles