I'm using the mediastore content provider to display information about the music on my device using a cursoradapter in a listview. I am trying to get the artist name, song name, and the album_art to display in my list buy i'm having trouble consolidating this information into one cursor from mediastore.audio.albums.albumart and mediastore.audio.media . I can get one or the other to display just fine but since cursoradapter only deals with one cursor I need to find a way to query all the information at once from the mediastore.
I have searched for the appropriate solution to this problem for ages but i haven't been able to get anything to work. The most relevant example that i could find (on which i based this attempt) is here. Obviously i don't fully understand the cursorloader constructor syntax. I've read posts elsewhere suggesting the use of cursorwrapper or joincursor to get the desired results ..but I'm stumped. If someone could point me in the right direction I would be very grateful. Here's what i have right now:
public class Playlist extends Activity {
private static final int CAFLAGS = 1;
private int listlayout = R.layout.playlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(listlayout);
Context context = this.getApplicationContext();
String[] projection = {
MediaStore.Audio.Media._ID,
"Title",
"Artist",
"Album",
"AlbumArt"
};
String selection = "Title="+MediaStore.Audio.Media.TITLE
+", Artist="+MediaStore.Audio.Media.ARTIST
+", Album="+MediaStore.Audio.Media.ALBUM
+", AlbumArt="+MediaStore.Audio.Albums.ALBUM_ART;
Uri queryUri = MediaStore.Files.getContentUri("external");
CursorLoader cursorloader = new CursorLoader(
this,
queryUri,
projection,
selection,
null,
"Title DESC"
);
Cursor cursor = cursorloader.loadInBackground();
TextView test = (TextView)findViewById(R.id.test);
test.setText(String.valueOf(cursor.getCount()));
ListView listview = (ListView)findViewById(android.R.id.list);
PlaylistAdapter adapter = new PlaylistAdapter(context,cursor,CAFLAGS);
listview.setAdapter(adapter);
}
}
my playlist adapter:
public class PlaylistAdapter extends CursorAdapter {
private LayoutInflater inflater;
public PlaylistAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameview = (TextView) view.findViewById(R.id.songname);
TextView artistview = (TextView) view.findViewById(R.id.artistname);
ImageView albumart = (ImageView)view.findViewById(R.id.albumcover);
String coverpath = cursor.getString(cursor.getColumnIndex("AlbumArt"));
String name = cursor.getString(cursor.getColumnIndex("Title"));
String artist = cursor.getString(cursor.getColumnIndex("Artist"));
Bitmap art = BitmapFactory.decodeFile(coverpath);
nameview.setText(name);
artistview.setText(artist);
albumart.setImageBitmap(art);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.from(context).inflate(R.layout.playlist_row, parent, false);
}
}
Okay so i thought this was some mystical processs but it is exactly like any other SQL query. The select columns will be useful picking certain data and putting it into my cursor. It seems that the reason this is not possible is because the table in which the album_art resides and that of the song title are different and it seems they cannot be accessed using one query. So i guess the solution would be to either use curserjoin or write the values that i want from the separate tables into my own SQLite database or matrix cursor. The second option would be more useful i think in the long run. I've searched but have been unable to find any examples of this, perhaps iterating through it using a do..while(cursor.hasnext) then add a row to a database that already has the correct column names.
How can i iterate through the columns in each row of my cursor and assign the values to a database that i define?