1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android Content Provider

Discussion in 'Android Development' started by vag, Sep 3, 2011.

  1. vag

    vag Newbie
    Thread Starter

    I want to get all music files titles with content provider, but I get SQL error. Here is the code




    package player.org;
    import java.io.File;

    import java.io.FileFilter;
    import java.io.FilenameFilter;
    import java.util.ArrayList;


    import android.R.id;
    import android.app.Activity;
    import android.app.ActivityGroup;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TabHost;

    public class AllSongs extends Activity{
    static ArrayList<String> listItem;
    ArrayAdapter<String> adapter;
    private final String mediaPath="./sdcard";
    ListView list;
    static Intent intent;
    static int currentPosition;
    static String s;


    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    //p=new Player();
    Log.d("allsongs","onCreate");
    listItem=new ArrayList<String>();
    setContentView(R.layout.list);
    adapter=new ArrayAdapter<String>(this,R.layout.list_item,listItem);
    list=(ListView)findViewById(R.id.list);
    Log.d("ata", "list="+ list);
    list.setAdapter(adapter);
    list.setTextFilterEnabled(true);
    updateSongsList();


    public void updateSongsList()
    {

    String [] colums=new String [] {
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.DATA
    };

    Cursor mediaCursor=managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, null, null,
    null, null);
    if(mediaCursor.moveToFirst())
    {
    int artistColum=mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
    String name;
    do{
    name=mediaCursor.getString(artistColum);
    listItem.add(name);
    } while(mediaCursor.moveToNext());
    adapter.notifyDataSetChanged();
    }
    }


    and here is the error

    09-03 03:18:29.379: ERROR/CursorWindow(378): Bad request for field slot 0,-1. numRows = 2, numColumns = 5
    09-03 03:18:29.379: DEBUG/AndroidRuntime(378): Shutting down VM
    (...)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.database.CursorWindow.getString_native(Native Method)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.database.CursorWindow.getString(CursorWindow.java:329)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at player.org.AllSongs.updateSongsList(AllSongs.java:107)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at player.org.AllSongs.onCreate(AllSongs.java:53)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    09-03 03:18:29.409: ERROR/AndroidRuntime(378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
     



    1. Download the Forums for Android™ app!


      Download

       
  2. vag

    vag Newbie
    Thread Starter

    I just can figure, why error accures?
     
  3. miXer

    miXer Android Enthusiast

    You are setting the adapter to the listview before the ArrayList is filled.
    First fill the ArrayList, then create the adapter from the ArrayList and give the adapter to the list.
     
    vag likes this.
  4. vag

    vag Newbie
    Thread Starter

    Thanks for reply. I found the problem. I just quering wrong provider, the uri must be MediaStore.Audio.Media. EXTERNAL_CONTENT_URI.
     
Loading...
Similar Threads - Android Content Provider
  1. Rik Deschamps
    Replies:
    4
    Views:
    258
  2. wmw_hv
    Replies:
    0
    Views:
    193
  3. Harbut
    Replies:
    0
    Views:
    219
  4. Jeff Jot
    Replies:
    11
    Views:
    359
  5. Taewendy
    Replies:
    2
    Views:
    318
  6. Novice_Independence
    Replies:
    1
    Views:
    217
  7. Advait
    Replies:
    4
    Views:
    412
  8. annie333
    Replies:
    6
    Views:
    294
  9. MyPoorAndy
    Replies:
    1
    Views:
    622
  10. Chen Dol Dawd
    Replies:
    1
    Views:
    1,257

Share This Page

Loading...