Search code examples
androidsqlitecursorandroid-sqlite

What's in Android cursor index -1?


I have just started Android.

So I'm using SQLite for an apps, and comes to something like

Cursor c = new Cursor()
c = db.rawquery(randomQueryString, null)

if (c!=null) 
    c.moveToFirst();

So, I have read somewhere that cursor default position is -1, and it is, according to the code above, not null, then what kind of data could possibly be in this position?


Solution

  • This the position "before first", being in this position make it more simple and clear to write a code to iterate thorough data in this cursor, you need just start looping from first as you would do in any other position.

    Example:

    if (cursor == null) {
        //handle
    }
    while (cusor.moveToNext()) {
        //do somehitng
    }