代码之家  ›  专栏  ›  技术社区  ›  Sujeet Kumar Mehta

游标没有给出Android中的列计数

  •  0
  • Sujeet Kumar Mehta  · 技术社区  · 14 年前

    我试图获得字符串数组中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 回复  |  直到 14 年前
        1
  •  1
  •   Aprian    14 年前

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

    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());
    }
    
        2
  •  0
  •   Rajendra    14 年前

    为什么使用For循环,使用do whole循环作为

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