How do I get the _count in my content provider?

后端 未结 4 837
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 17:32

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

4条回答
  •  再見小時候
    2020-12-13 18:15

    With cursor.getCount() you can not assure that it returns the real number of items returned. There are much better ways:

    1- If you are using Content Providers, you can do a query and use the Column (_COUNT) included in BaseColumns for your projection

    @Override
    public Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        ...
    
        projection = new String[] {
            ContentContract.NotificationCursor.NotificationColumns._COUNT,
        };
    
        ...
    
        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);
        return cursor;
    }
    

    2- To do a rawQuery using SELECT COUNT(*) as @saurabh says in his response.

提交回复
热议问题