Question

I am very new android developer and i'm trying to fill a autocompletetextview with contact display names, and when a suggestion is clicked i want it to autofill the email field from that contact. I managed to get it working, but my problem is speed. Because I need to get the email address of all contacts, I need to do a query for every single contact, which takes a very long time when I have about 3000 contacts on the device i'm testing on. The thing is, most of the contacts don't even have an email address, but I still need to do a query to discover that. I am getting the contacts via an async task. Here is the doInBackground of the AsyncTask:

    protected Object[] doInBackground(ContentResolver...cr) {

    try{
        List<String> names = new ArrayList<String>();
        Map<Integer, String> emails = new HashMap<Integer, String>();
        Map <Integer, List<String>> contacts = new HashMap<Integer, List<String>>();
        /*********** Reading Contacts Name **********/


        //Query to get contact name

        Cursor cur = cr[0]
                .query(ContactsContract.Contacts.CONTENT_URI,
                        null,
                        null,
                        null,
                        null);
        // If data data found in contacts 
        if (cur.getCount() > 0) {


            Log.i("AutocompleteContacts", "Reading   contacts........");

            String name = "";
            String id = "";

            while (cur.moveToNext()) 
            {
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String email = "";
                if(name != null)
                {
                    names.add(name.toString());
                }
                Cursor cur1 = cr[0].query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
                if(cur1.getCount()>0)
                {
                    while(cur1.moveToNext())
                    {
                        email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        if(email != null)
                        {
                            emails.put(Integer.parseInt(id), email);
                        }
                    }
                }
                cur1.close();
                List<String> line = new ArrayList<String>();
                line.add(name);
                line.add(email);
                contacts.put(Integer.parseInt(id), line);
            }  // End while loop

            } // End Cursor value check
        else
        {
            Log.i("contacts", "No contacts found");
        }
        cur.close();

        results[0] = names;
        results[1] = contacts;
        results[2] = emails;
    } catch (NullPointerException e) {
        Log.i("AutocompleteContacts","Exception : "+ e);
    }
    return results;
}

Basically my question is: Is there a way to check if the contact has an email without another query like HAS_EMAIL_ADDRESS column or something.

If you want to view the full source of my app: https://github.com/michael-elgavi/perlib

Était-ce utile?

La solution

After thinking for a while a realized I only need the email of the contact that you click on, so I just removed the email code from the asynctask and made another asynctask that loads an email of a specific contact. Then I get the email of the contact that was clicked and fill the email field. Code can be found in the repository I linked to in the question.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top