Stephen
Stephen

Reputation: 762

Using CursorLoader to get contacts causes duplication

I am building an activity which I want to populate with contacts that have telephone numbers.

I am not using an Intent because I want there to be a checkbox before each contact in my listview.

I am using CursorLoader. Here is some code from the onCreateLoader method;

String projection[] = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
    Uri uri = ContactsContract.Data.CONTENT_URI;

    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1" + 
    " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " =1";

    String order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    CursorLoader loader = new CursorLoader(this, uri, projection, selection, null, order);

I use a subclass of ResourceCursorAdapter with my ListView.

Does anyone have any ideas how I remove the duplicates? I don't want to use a Set unless I really have to.

Upvotes: 1

Views: 2504

Answers (2)

devunwired
devunwired

Reputation: 63293

I would recommend querying the Contacts table as opposed to the raw Data table.

Uri uri = ContactsContract.Contacts.CONTENT_URI;

The data table contains raw contact data, which can mean multiple entries for the same "person" since Android contacts are aggregated from multiple different account sources. Everything else looks fine.

Upvotes: 5

Flynn81
Flynn81

Reputation: 4168

If you have already verified that the data returned by your cursor is not doubling results (and that content provider does not have duplicates), the problem is most likely in your implementation of newView and bindView. Make sure that newView just returns a "blank" view, meaning no values for TextViews or ImageViews have been set. Set those values in bindView. Most likely bindView isn't correctly setting the values in your views.

Upvotes: 1

Related Questions