1

I'm able to fetch all the contacts from Contact list in android(phone numbers and emails) but fetching all of them takes long time. To speed up this I have stored them once in my application. But now i can't get the updated contacts. How does they need to be synced with my application?

Problem arises when I try to display them in a list because a user database of phone numbers and emails exceeds the 2000. I'm currently using base adapter implementation for this. Can somebody help me out for large contact database?

Application like GroupMe n Viber show all user contacts very fast can somebody tell me brief explanation to achieve it.

String KEY_NAME = "Name";
    String KEY_NO   = "No";

    String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = 1";
    String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    String data="";
    String name="";
    ContactEntry contactObj;
    String id;
    String index="";


    final String[] projection = new String[]{ContactsContract.Contacts._ID , ContactsContract.Contacts.DISPLAY_NAME , ContactsContract.Contacts.HAS_PHONE_NUMBER};

    final String[] email_projection = new String[] {ContactsContract.CommonDataKinds.Email.DATA , ContactsContract.CommonDataKinds.Email.TYPE};

    final String[] phone_projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};

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

    if(cur.getCount()>0){

        while(cur.moveToNext()){

             id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , phone_projection , 
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);


                while (pCur.moveToNext()){

                         data = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if(!temp.contains(data) && !data.equals(null)){
                            //Adding PhoneNumbers in List   
                        }
                } 
                    pCur.close();
            }

           Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, email_projection,
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  new String[]{id}, null); 


           while (emailCur.moveToNext()){ 

                data = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                if(!temp.contains(data) && !data.equals(null)){ 
                                  //Adding Email in List
                }
            } 
            emailCur.close();
        }
    }

I have used the above code How can i pass the Curser in CurosrAdaper for all Emails and Phonenumbers. please help me i havn't found any solution.

Khushwant
  • 305
  • 2
  • 6
  • 17

3 Answers3

1

Android provides a sync adapter to solve this problem. you may refer to this example for the implementation. http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

Amol Gupta
  • 704
  • 1
  • 8
  • 26
1

i had same issue , i have used ResourceCursorAdapter which fetches contacts as fast as Contact API from android....

Richa
  • 3,165
  • 1
  • 22
  • 26
0

I think you need to have a service (or) Asynch task something like that which periodically updates contacts from phone to your local app. Otherwise you can't synch.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • hi that will be very expensive , instead of that i think every time when app start it refresh it's contacts or reload contacts instead of storing them.please suggest a way for for showing them in a list for my above code. – Khushwant Feb 03 '12 at 13:56