如何使用cursorloader获取特定专辑的歌曲?

时间:2013-08-12 08:10:13

标签: java android android-cursorloader

我试图在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的光标设置到数据库中的特定位置的方法? 我想对游标加载器使用类似的逻辑。 请帮我解决这个问题。 如果需要进一步澄清,请告诉我?

0 个答案:

没有答案