Search code examples
androidarraylistmediaandroid-contentprovider

How to display the media title in a listview?


How can I get media titles from COntentResolver. i tried but it no works, i want to display media titles in my listView .

Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
      int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
      do {
        String T = cursor.getString(ind);
        abcl.add(T);
        Log.i("SOngName", T);
      } while (cursor.moveToNext());
    }

but this gives me error of IndexOutOfBondsExceptions, my error logcat is

please any one guide , thanks


Solution

  • well first of all you should go for Cursor Documentation in android . it will tell you that after using the cursor is not null check; you have to move cursor to the start of first row on incoming result set.

    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    if (cursor != null) {
    if(cursor.MoveToFirst()){
          int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
          do {
            String T = cursor.getString(ind);
            abcl.add(T);
            Log.i("SOngName", T);
          } while (cursor.moveToNext());
      }
    }