Search code examples
androidandroid-asynctaskandroid-cursorandroid-cursorloader

android cursor in infinite loop


I have cursor that comes from a cursor loader. The problem is if I try to move through that cursor it will not move foward and puts in a endless loop.

  public void onLoadFinished(Loader<Cursor> loader, final Cursor cursor) {
    Log.d(TAG, "CALLED onLoadFinished");
    ...
    else if(loader.getId() == LOADER_1)
    {
        while(cursor.moveToFirst())
        {
              Log.d(TAG, "LOOPING");
              cursor.moveToNext();
        }
    }
 }

Solution

  • Your current while loop will not work. Right now you're moving the cursor to the first position in the while condition then inside thewhile block you're pushing the cursor to the next position. As the while block is finished the condition will be tested again so cursor.moveToFirst() will be called again. This will continue again and again. Basically you'll move from the first position to the second position of the cursor in an infinite loop.

    The loop should be like this:

    while(cursor.moveToNext()) {
         Log.d(TAG, "LOOPING");
         // do other stuff 
         // each time you'll have a new row from the cursor
    }