alandarev
alandarev

Reputation: 8635

Cursor.moveToNext without moveToFirst

Will the next code work as expected?

Cursor c = db.query(tableName, requestedColumns, condition,
        conditionParams, null, null, sortOrder);
while(c.moveToNext())   {
     //do stuff with rows
}

The examples I found so far suggest calling c.moveToFirst() prior looping, but is it really necessary?

Upvotes: 9

Views: 3808

Answers (3)

Stephen Bridgett
Stephen Bridgett

Reputation: 671

A late answer, but maybe useful to others finding this page. I've been using an: "if () do { } while();" loop. The "moveToFirst()" function returns false if no rows were returned in the cursor. The "moveToNext()" function returns false when past end of the cursor:

Cursor c = db.query(......);

if (c.moveToFirst()) do {
   data = c.getString(0) // eg. get value from first column in cursor
   // do something more
} while (c.moveToNext());

c.close();

Upvotes: 2

Tomer Mor
Tomer Mor

Reputation: 8028

Yes, it will work, moveToNext actually call moveToFirst

Upvotes: 12

Guillermo Merino
Guillermo Merino

Reputation: 3257

Yes, it will work, the first moveToNext() will point the cursor to the first entry of the result set, (if the result set has values)

Upvotes: 3

Related Questions