Following up on https://stackoverflow.com/a/5935434, I'm trying to get the total number of contact items from Android's address book, in the most efficient manner.
I know that we can select all contacts and then count the results as follow:
int count = 0;
var ctx = Application.Context;
var countCursor = ctx.ApplicationContext.ContentResolver.Query(
ContactsContract.Contacts.ContentUri,
null,
null,
null,
null
);
if (countCursor != null)
{
count = countCursor.Count;
countCursor.Close();
}
return count;
But I was thinking that only requesting for the count would me more efficient and consume less resources from the device.
But, as in https://stackoverflow.com/a/5935434/1990692, the following code:
int count = 0;
var ctx = Application.Context;
var countCursor = ctx.ApplicationContext.ContentResolver.Query(
ContactsContract.Contacts.ContentUri,
new String[] { "COUNT(*) AS c" },
null,
null,
null
);
if (countCursor != null) {
countCursor.moveToFirst();
count = countCursor.getInt(0);
countCursor.Close();
}
return count;
does not work and throws a Java.Lang.IllegalArgumentException
with the following message: Non-token detected in 'count(*) AS c'
What am I doing wrong?
I'm using Xamarin.Android hence this is c# but I assume this would be the same in java for native Android.