Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the _count in my content provider?

What should I do to get my content provider to return the _count column with the count of records? The documentation says it is automatic, but maybe it's only taking about some built-in content provider. Running a query to the database seems not to return it.

like image 603
pupeno Avatar asked Jan 22 '09 06:01

pupeno


People also ask

How can you find out the number of items returned by a content provider query?

If you are using ContentProvider. query() a Cursor is returned. Call Cursor. getCount() to get a count of records in the returned cursor.

How do I access Android content provider?

Accessing a provider. When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider .

How do you initialize a content provider?

The primary methods that need to be implemented are: onCreate() which is called to initialize the provider. query(Uri, String[], Bundle, CancellationSignal) which returns data to the caller. insert(Uri, ContentValues) which inserts new data into the content provider.


1 Answers

If you are using contentProvider then you have to do it like count(*) AS count.

If you use cursor.getCount(), that would not be as efficient as the above approach. With cursor.getCount() you are fetching all the records just to get counts. The entire code should look like following -

 Cursor countCursor = getContentResolver().query(CONTENT_URI,                 new String[] {"count(*) AS count"},                 null,                 null,                 null);          countCursor.moveToFirst();         int count = countCursor.getInt(0); 

The reason why this works is because android needs a column name to be defined.

like image 83
saurabh Avatar answered Oct 05 '22 16:10

saurabh