While learning to iterate over a cursor, I learned that I needed to first move to position "-1" and then use "moveToNext" in a loop:
cursor.moveToPosition(-1);
for (int i = 0; cursor.moveToNext(); i++) {
//do something with the cursor
}
While mathematically this makes sense, I don't know what it means to move to a cursor to a negative position. The documentation just says it's valid–doesn't seem to say how it's used.
Is this used ONLY to make iteration possible, or is there other use cases for the position -1?
cursor.moveToFirst()
aka 0 – ColonizeI learned that I needed to first move to position "-1"
I don't see much in the linked question that could lead you to believe that. Use moveToFirst and a while loop. – BarrybarrymoremoveToPosition(-1)
followed by a while loop, and, (b)moveToFirst
followed by a do-while loop. – CustombuiltmoveToPosition(-1)
to be clean, as it involves a magic number. AmoveToBeforeFirst
is really missing. – BarrybarrymoremoveToFirst
+ do-while loop won't work either, I just realized. If there are no items in the cursor, the do-while loop will still start an iteration and end up throwing an error. My mistake there. So that leaves option (a) only. – Custombuilt