1

Background

I have a cursor used as follows:

        for (int n=0; n<num_songs; n++) {
            boolean isChecked = checked_positions.get(n);
            if (isChecked) {
                Cursor cursor = (Cursor) getListView().getItemAtPosition(n);
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                cursor.close();
                //save artist and title strings to file...
            }
        }

This gives a StaleDataException on the second time round the loop when it tries to reuse the closed cursor. If I remove cursor.close() it runs fine but I get a "Cursor finalized without prior close" warning.

Research

The advice in this answer: https://stackoverflow.com/a/18107638/1977132 is to set the cursor to null cursor.close(); cursor = null; presumably so a new cursor can be created, but it makes no difference, the second time round the loop it still gives a StaleDataException.

I've already tried...

I tried moving it outside the loop as follows:

        Cursor cursor;
        for (int n=0; n<num_songs; n++) {
            boolean isChecked = checked_positions.get(n);
            if (isChecked) {
                cursor = (Cursor) getListView().getItemAtPosition(n);
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                //save artist and title strings to file...
            }
        }
        cursor.close();

but this wouldn't compile with the error "cursor might not have been initialized".

Question

My question is how do I correctly use and close a cursor within a loop.

Community
  • 1
  • 1
user1977132
  • 487
  • 2
  • 18
  • whilst the answers below solved the "Cursor finalized without prior close" warning, they introduced a new problem when the fragment was backgrounded. See the following question: http://stackoverflow.com/q/32588271/1977132 – user1977132 Sep 15 '15 at 14:11

2 Answers2

1

Try this

Cursor cursor = null;
for (int n=0; n<num_songs; n++) {
    boolean isChecked = checked_positions.get(n);
    if (isChecked) {
        cursor = (Cursor) getListView().getItemAtPosition(n);
        String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
        String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
        //save artist and title strings to file...
    }
}
if(cursor != null)
    cursor.close();
Adeel Ahmad
  • 939
  • 1
  • 10
  • 20
1

This is the proper way to close cursor:

    Cursor cursor = null;
    try{
        for (int n=0; n<num_songs; n++) {
            boolean isChecked = checked_positions.get(n);
            if (isChecked) {
                cursor = (Cursor) getListView().getItemAtPosition(n);
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                //save artist and title strings to file...
            }
        }
    } finally {
        if(cursor != null){
            cursor.close();
        }
    }
user2203031
  • 402
  • 3
  • 14