3

我正在尝试在 android 中使用 CursorLoaders 创建一个基本的音乐播放器。这个想法是有两个列表视图的活动,一个将显示专辑,另一个显示特定选定专辑的歌曲。

以下是我的主要活动的代码:

public class AlbumCursorLoader extends Activity implements LoaderCallbacks<Cursor> {
private static final int LOADER_ALBUM_ID = 1;
private static final int LOADER_SONGS_ID = 2;

AlbumsAdapter mAdapter;
AlbumSongsAdapter mSongsAdapter;

CursorLoader AlbumCursor;
CursorLoader SongsCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_album_cursorloader);

    mAdapter = new AlbumsAdapter(this, null);
    mSongsAdapter = new AlbumSongsAdapter(this,null);

    ListView AlbumList = (ListView) findViewById(R.id.listviewid_albumart);
    ListView SongsList = (ListView) findViewById(R.id.listviewid_albumsongs);
    AlbumList.setAdapter(mAdapter);
    SongsList.setAdapter(mSongsAdapter);

    getLoaderManager().initLoader(LOADER_ALBUM_ID, null, this);
    getLoaderManager().initLoader(LOADER_SONGS_ID, null, this);

    AlbumList.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
            //Have to display the songs on clicking the album list.
        }
    });
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {      
    String select = null;

    switch(id){
    case LOADER_ALBUM_ID:
        String[] mProjection =
        {
                MediaStore.Audio.Albums._ID,
                MediaStore.Audio.Albums.ALBUM_ART,
                MediaStore.Audio.Albums.ALBUM_KEY
        };
        AlbumCursor = new CursorLoader(this, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,mProjection, select, null, null);
        return AlbumCursor;
    case LOADER_SONGS_ID:
        String[] mProjection1 =
        {
              MediaStore.Audio.Media.DATA,
              MediaStore.Audio.Media._ID,
              MediaStore.Audio.Media.TITLE,
              MediaStore.Audio.Media.DISPLAY_NAME,
              MediaStore.Audio.Media.MIME_TYPE,
        };
        SongsCursor = new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,mProjection1, select, null, null);
        return SongsCursor;
    default:
        return null;
    }
}  

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    switch (loader.getId()){
    case LOADER_ALBUM_ID:
        mAdapter.swapCursor(data);
    break;
    case LOADER_SONGS_ID:
        mSongsAdapter.swapCursor(data);
    break;
    }
}  

public void onLoaderReset(Loader<Cursor> loader) {
    switch (loader.getId()){
    case LOADER_ALBUM_ID: 
        mAdapter.swapCursor(null);  
    break;
    case LOADER_SONGS_ID: 
        mSongsAdapter.swapCursor(null);  
    break;
    }
}  

}

这是自定义适配器的代码:

public class AlbumsAdapter extends CursorAdapter {
private final LayoutInflater mInflater;

 public AlbumsAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater=LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ImageView albumArt =(ImageView)view.findViewById(R.id.albumart);
    String art = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    Drawable img1 = Drawable.createFromPath(art);
    albumArt.setImageDrawable(img1);

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view=mInflater.inflate(R.layout.album_art,parent,false); 
    return view;
}


}

现在我的疑问是当我单击该专辑时如何获取特定专辑的歌曲?我知道如何通过硬编码专辑名称来查询特定专辑的歌曲,但是如何在点击特定专辑时获取专辑歌曲?它应该是动态的。

我尝试了另一种方法,使用普通光标(没有光标加载器),将光标设置到特定位置 cursor.moveToPosition(position); 在 ListActivity 的 onListItemClick 中。

     protected void onListItemClick(ListView l, View v, int position, long id) {
        if (cursor.moveToPosition(position)) {

          String[] columns = { MediaStore.Audio.Media.DATA,
              MediaStore.Audio.Media._ID,
              MediaStore.Audio.Media.TITLE,
              MediaStore.Audio.Media.DISPLAY_NAME,
              MediaStore.Audio.Media.MIME_TYPE, };

          String where = android.provider.MediaStore.Audio.Media.ALBUM
              + "=?";

          String whereVal[] = { cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };

          String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

          cursor = managedQuery(
              MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
              where, whereVal, orderBy);

          String[] displayFields = new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
          int[] displayViews = new int[] { android.R.id.text1 };
          setListAdapter(new SimpleCursorAdapter(this,
              android.R.layout.simple_list_item_1, cursor,
              displayFields, displayViews));

        }
      }

类似的是将 cursorloader 的光标设置到数据库中特定位置的方法吗?我想对游标加载器使用类似的逻辑。请帮我解决这个问题。请让我知道是否需要进一步澄清?

4

0 回答 0