0

我想扫描 sdcard 中存在的所有歌曲并将它们显示在列表中。

目前我的方法是:-

  1. 创建一个 asyncTask 类
  2. 在 doInBackground 方法中扫描所有媒体文件
  3. 在 post 中执行 get ListView 和 setCursorAdapter 到这个 ListView

在我的自定义光标适配器中,我有 3 种方法:- 4. BindView 5. newView 6. getView (我有 onItemClickListener )

我在这里有两个问题:- 当我的歌曲超过 450 首时,它不会扫描所有歌曲并仍显示 450 个列表项

OnClickListener 带来 Error -> Cannot map BpMemoryHeap to Heap。07-18 18:00:56.977: 错误/JavaBinder(1620):未捕获的远程异常!(跨进程尚不支持异常。) *

以下是我的异步任务:-

私有类 FetchAllMusic 扩展 AsyncTask { ArrayList 轨道;光标光标,cursorAlbums;

    @Override
    protected void onPreExecute() {
        myWaitDialog.run();
        super.onPreExecute();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Byte doInBackground(Void... params) {
            String[] projection = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,          MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DURATION
                                 };
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
     cursor = PlaylistScreen.this.managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
                selection, null, null);
        cursor.moveToFirst();

          cursorAlbums = null;
            if(cursor.moveToFirst()){
                do{
                 String[] projection1 = { MediaStore.Audio.Albums.ALBUM_ART };
                    String selection1 = MediaStore.Audio.Albums._ID + " =="
                            + cursor.getString(6);

                     cursorAlbums = PlaylistScreen.this.managedQuery(
                             MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, /* projection */
            projection1, selection1, null, null);

                }while(cursor.moveToNext());
            }

            return null;
    }

     protected void onPostExecute(Byte result) {

        myWaitDialog.dismiss();
        layoutAllMusic = (LinearLayout) findViewById(R.id.l_all_music);
        try{
        Comparator comparator = Collections.reverseOrder();
            System.out.println(tracks);
                //Collections.sort(tracks, comparator);
                }catch(Exception e){

            e.printStackTrace();
        }
    Log.d("Before List Adapter","+++++++++++++++++");
      PlaylistScreen.this.lv = getListView();


    MusicCursorAdapter musicCursor = new MusicCursorAdapter(PlaylistScreen.this, cursor, cursorAlbums);
       PlaylistScreen.this.lv.setAdapter(musicCursor);
      super.onPostExecute(result);
    }
}
4

1 回答 1

0

确保关闭所有使用的游标......

cursor = PlaylistScreen.this.managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);
    cursor.moveToFirst();

      cursorAlbums = null;
        if(cursor.moveToFirst()){
            do{
             String[] projection1 = { MediaStore.Audio.Albums.ALBUM_ART };
                String selection1 = MediaStore.Audio.Albums._ID + " =="
                        + cursor.getString(6);

                 cursorAlbums = PlaylistScreen.this.managedQuery(
                         MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, /* projection */
        projection1, selection1, null, null);

            }while(cursor.moveToNext());
        }
        cursor.close();
        return null;

并再次尝试关闭其他两个游标(musicCursor、cursorAlbums)。希望它应该工作正常。

于 2011-08-02T10:59:50.697 回答