How to retrieve data from cursor class

I need to know how to retrieve data from cursor. I need this because the ringtonemanager returns all the audio files in form of cursor object, I need to know how to retrieve the values.

Anbudan.

  • listener for pressing and releasing a button
  • Detecting state changes made to the BluetoothAdapter?
  • Webview email link (mailto)
  • How to increase hit area of Android button without scaling background?
  • Android Java UTF-8 HttpClient Problem
  • Cannot call getSupportFragmentManager() from activity
  • Android: How can I add HTML links inside a ListView?
  • Android Emulators for Specific Devices
  • The right way to make cross-platform RTMP streaming/playing app. Phonegap livu plugin?
  • android-How to run Service in different thread than main thread?
  • Android overflow menu positioned above actionbar/toolbar?
  • Android - how to get android.R.anim.slide_in_right
  • 3 Solutions collect form web for “How to retrieve data from cursor class”

    Once you have the Cursor object, you can do something like this:

    if (cursor.moveToFirst()){
       do{
          String data = cursor.getString(cursor.getColumnIndex("data"));
          // do what ever you want here
       }while(cursor.moveToNext());
    }
    cursor.close();
    

    This looks a bit better:

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        ...
    }
    

    Salvador’s answer will continue to fetch data from the row after the last row because moveToNext() will only return false when the cursor is pointing at the row after the last row. It will continue to iterate even if the cursor is pointing at the last row.

    The correct template should be:

    if (cursor.moveToFirst()){
       while(!cursor.isAfterLast()){
          String data = cursor.getString(cursor.getColumnIndex("data"));
          // do what ever you want here
          cursor.moveToNext();
       }
    }
    cursor.close();
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.