Monday, 12 August 2013

How to fetch songs of an particular album using cursorloaders?

How to fetch songs of an particular album using cursorloaders?

I'm trying to create a basic Music player using CursorLoaders in android.
The idea is to have activity with two listviews, one will display the
albums & another displays the songs of a particular selected album.
below is the code of my main activity:
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;
}
}
}
and here is the code for custom adapter:
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;
}
}
Now my doubt is how to get the songs of an particular album when i click
on that album? I know how to query a particular album for songs by hard
coding the album name, but how to fetch an album songs on clicking on
particular album? It should be dynamic.
I've tried an alternative way by using normal cursor(without
cursorloader),by setting the cursor to a particular position
cursor.moveToPosition(position); in onListItemClick of an ListActivity.
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));
}
}
Similarly is the way to set the cursorloader's cursor to a particular
position in database? i want to use similar kind of logic for
cursorloaders. Please help me in resolving this problem. Kindly let me
know if any further clarification is required?
Thanks in advance.

No comments:

Post a Comment