How do I get the _count in my content provider?

后端 未结 4 830
没有蜡笔的小新
没有蜡笔的小新 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:12

    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.

提交回复
热议问题