光标没有给出Android中的列数

时间:2012-09-06 07:22:55

标签: 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[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(); }

2 个答案:

答案 0 :(得分:0)

为什么使用For循环,使用do while循环

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

          }while (cursor.moveToNext());
    }

或使用while循环

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

                        // get the data from here

                       mCursor.moveToNext();
                    }
               }

答案 1 :(得分:0)

试试这个,应该做你想做的事情

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());
}