Look at your logic. First, you move to the first record. Immediately after that, you moveToNext()
. The first item is being skipped.
A few options:
Move the moveToNext()
call to the end of the loop:
int counter = 0;
if (cursor.moveToFirst())
do {
contact_ids_to_skip.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
counter++;
} while(cursor.moveToNext())
Change moveToFirst()
to moveToPosition(-1)
:
int counter = 0;
if (cursor.moveToPosition(-1))
while (cursor.moveToNext()) {
contact_ids_to_skip.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
counter++;
}
Or, simply get rid of moveToFirst()
altogether:
int counter = 0;
while (cursor.moveToNext()) {
contact_ids_to_skip.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
counter++;
}
The last works because when a Cursor
is returned from any query method, it's position is -1, which is "before the first item". So, a moveToNext()
puts it in the right spot. Only use that one if the Cursor has just been returned from a query, though. If its position has been changed, use one of the first two methods.