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.

  • How to change the style of Date picker in android?
  • how to make http get request in Android
  • Android : Putting Grid of dynamic&custom objects inside another Grid of dynamic&custom objects
  • Is it possible to run an Android emulator without setting up Eclipse
  • android splitting with space not working for this case. Why?
  • How to send hashmap value to another activity using an intent
  • Related posts:

    jQuery Drag and Drop on touch devices (iPad, Android)
    Android error randomly java.lang.NoClassDefFoundError: com.facebook.internal.Utility
    Using icon in top left of ActionBarSherlock to navigate
    Samsung hovering API with finger
    Ionic on Windows: Android SDK installed. Still get “Error: Please install Android target: ...
    Preferred way to attach AudioEffect to global mix?
  • Custom Fonts not working in lollipop?
  • TimePicker in PreferenceScreen
  • How to solve build error for product flavor, when a referenced but unnecessary source/resource not found?
  • Meaning of red color in Android's Nine-patch drawables
  • android.R.color.transparent not fully transparent
  • Possible to Override system .so library in App
  • 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.