tags:

views:

1006

answers:

3

Android's API provides a clean mechanism via SQLite to make queries into the contact list. However, I am not sure how to limit the results:

Cursor cur = ((Activity)mCtx).managedQuery(
                People.CONTENT_URI,
                columns,
                "LIMIT ? OFFSET ?",
                new String[] { Integer.toString(limit), Integer.toString(offset) },
                null
        );

Doesn't work.

+1  A: 

I think you have to do this sort of manually. The Cursor object that is returned from the managedQuery call doesn't execute a full query right off. You have to use the Cursor.move*() methods to jump around the result set.

If you want to limit it, then create your own limit while looping through the results. If you need paging, then you can use the Cursor.moveToPosition(startIndex) and start reading from there.

Andrew Burgess
+2  A: 

You are accessing a ContentProvider, not SQLite, when you query the Contacts ContentProvider. The ContentProvider interface does not support a LIMIT clause directly.

If you are directly accessing a SQLite database of your own, use the rawQuery() method on SQLiteDatabase and add a LIMIT clause.

CommonsWare
A: 

Actually, depending on the provider you can append a limit to the URI as follows:

uri.buildUpon().appendQueryParameter("limit", "40").build()

I know the MediaProvider handles this and from looking at recent code it seems you can do it with contacts too.

dhaag23