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.
If you are using ContentProvider. query() a Cursor is returned. Call Cursor. getCount() to get a count of records in the returned cursor.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With