GensaGames
GensaGames

Reputation: 5788

CursorLoader loading contacts - Android

I'm using code below, and load contats. All is well, when contacts size less than 100. I use tab, where switch fragments. And when click on tab with contact, i get the delay.But i use CursorLoader, and do not understand why there is a delay.

Using this class to load contacts (ContactFetcher.fetchAll();)

UPDATE!

There is a way to upload contacts using the LoaderManager, with list phone number (if the contact has more than one) and list email? It would be great with an example)

Here is code:

public class ContactFetcher {
    private Context context;

    public ContactFetcher(Context c) {
        this.context = c;
    }

    public ArrayList<MainContact> fetchAll() {
        ArrayList<MainContact> listContacts = new ArrayList<MainContact>();
        CursorLoader cursorLoader = new CursorLoader(context, RawContacts.CONTENT_URI,
                null, // the columns to retrieve (all)
                null, // the selection criteria (none)
                null, // the selection args (none)
                null // the sort order (default)
        );

        Cursor c = cursorLoader.loadInBackground();
        if (c.moveToFirst()) {
            do {
                MainContact contact = loadContactData(c);
                listContacts.add(contact);
            } while (c.moveToNext());
        }
        c.close();
        return listContacts;
    }

    private MainContact loadContactData(Cursor c) {
        // Get Contact ID
        int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
        String contactId = c.getString(idIndex);
        // Get Contact Name
        int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        String contactDisplayName = c.getString(nameIndex);
        MainContact contact = new MainContact(contactId, contactDisplayName, null, null, null, true);
        fetchContactNumbers(c, contact);
        fetchContactEmails(c, contact);
        return contact;
    }

    public void fetchContactNumbers(Cursor cursor, MainContact contact) {
        // Get numbers
        final String[] numberProjection = new String[]{Phone.NUMBER, Phone.TYPE,};

        Cursor phone = new CursorLoader(context, Phone.CONTENT_URI, numberProjection,
                Phone.CONTACT_ID + " = ?", new String[]{String.valueOf(contact.id)}, null)
                .loadInBackground();

        if (phone.moveToFirst()) {
            final int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER);
            final int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE);

            while (!phone.isAfterLast()) {
                final String number = phone.getString(contactNumberColumnIndex);
                final int type = phone.getInt(contactTypeColumnIndex);
                String customLabel = "Custom";
                CharSequence phoneType = Phone.getTypeLabel(
                        context.getResources(), type, customLabel);
                contact.addNumber(number, phoneType.toString());
                phone.moveToNext();
            }

        }
        phone.close();
    }

    public void fetchContactEmails(Cursor cursor, MainContact contact) {
        // Get email
        final String[] emailProjection = new String[]{Email.DATA, Email.TYPE};

        Cursor email = new CursorLoader(context, Email.CONTENT_URI, emailProjection,
                Email.CONTACT_ID + "= ?", new String[]{String.valueOf(contact.id)}, null)
                .loadInBackground();

        if (email.moveToFirst()) {
            final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);
            final int contactTypeColumnIndex = email.getColumnIndex(Email.TYPE);

            while (!email.isAfterLast()) {
                final String address = email.getString(contactEmailColumnIndex);
                final int type = email.getInt(contactTypeColumnIndex);
                String customLabel = "Custom";
                CharSequence emailType = Email.getTypeLabel(
                        context.getResources(), type, customLabel);
                contact.addEmail(address, emailType.toString());
                email.moveToNext();
            }

        }

        email.close();
    }
}

Upvotes: 0

Views: 1747

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

The issue is that you're not using CursorLoader properly. It should not be created or used directly in a method call such as fetchContactNumbers() which is presumably being called from an Activity or Fragment lifecycle callback like onResume(). Instead, you should use the LoaderManager.initLoader() method and provide a class which implements LoaderManager.LoaderCallbacks. In the onCreateLoader() callback is where you create your CursorLoader and in onLoadFinished() is where you retrieve the results. This article will help you to understand the Loader framework: http://po.st/xHoVMf

Upvotes: 4

Related Questions