游标未给出Android中的列计数

游标未给出Android中的列计数,android,android-cursoradapter,Android,Android Cursoradapter,我试图获取sd卡中字符串数组中所有歌曲的路径,但它没有存储在数组中,否则它会覆盖它, 请帮帮我,代码 String[] projection = new String[] {MediaStore.Audio.Media.DATA}; Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,projection, null, null, null); mCur.moveToFirst(); path=new String[

我试图获取sd卡中字符串数组中所有歌曲的路径,但它没有存储在数组中,否则它会覆盖它, 请帮帮我,代码

String[] projection = new String[] {MediaStore.Audio.Media.DATA};
    Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,projection, null, null, null);
    mCur.moveToFirst();
    path=new String[mCur.getColumnCount()]; 
    while (mCur.isAfterLast() == false) {       
    for (i=0; i<mCur.getColumnCount(); i++) 
    {path[i]=mCur.getString(i);
            System.out.println(",,,,,,,,"+mCur.getColumnCount());
            System.out.println(path[i]+i);}
            mCur.moveToNext(); }
String[]projection=newstring[]{MediaStore.Audio.Media.DATA};
游标mCur=managedQuery(媒体、外部内容、投影、null、null);
mCur.moveToFirst();
path=新字符串[mCur.getColumnCount()];
而(mCur.isAfterLast()==false){

对于(i=0;i为什么要使用for循环,请使用do-thill-loop作为

        if (cursor.moveToFirst()){
        do{
                       // get the data from here

          }while (cursor.moveToNext());
    }
                   if (mCursor != null) {
                   mCursor.moveToFirst();
                   while(!mCursor.isAfterLast()){

                        // get the data from here

                       mCursor.moveToNext();
                    }
               }
或者使用while循环作为

        if (cursor.moveToFirst()){
        do{
                       // get the data from here

          }while (cursor.moveToNext());
    }
                   if (mCursor != null) {
                   mCursor.moveToFirst();
                   while(!mCursor.isAfterLast()){

                        // get the data from here

                       mCursor.moveToNext();
                    }
               }

试试这个,你想做什么就做什么

String[] projection = new String[] {MediaStore.Audio.Media.DATA};
Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,projection, null, null, null);
// check if there is data returned or not
if (mCur.moveToFirst()) {
    // you should use getCount() rather than getColumnCount()
    // getCount() is the number of rows, while getColumnCount() is the number of columns returned
    // remember your projection?? only return 1 column.
    path = new String[mCur.getCount()]; 
    int whichColumn = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    int i = 0;
    do {
        path[i] = mCur.getString(whichColumn);
        i++;
    } while (mCur.moveToNext());
}

你的回答对我很有用,我也遇到了同样的问题。谢谢