1

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?

Community
  • 1
  • 1
spintron
  • 107
  • 10
  • so what do you have problems with? – pskink Nov 05 '14 at 10:50
  • @pskink My application dies on cursorloader.loadInBackground likely because my query is invalid. I'm curious if there is a way to query both of these mediastore sources at the same time to get a single cursor for use in a listview via a cursoradapter. – spintron Nov 05 '14 at 10:54
  • try to read ContentResolver.query() docs and you will know how to use CursorLoader: basically it uses the same params as query() method – pskink Nov 05 '14 at 10:58
  • A CursorLoader is used through the `LoaderManager`, if you're calling loadInbackground() on your own you'll be doing the query on the main UI thread. – user Nov 05 '14 at 11:00
  • Cool thanks for the suggestions i'll look into these stat. Apart from the issue of properly implementing a LoaderManager for my CursorLoader does it seem like i am on the right track as far as the query itself goes? – spintron Nov 05 '14 at 11:08
  • After properly setting up the LoaderManager life-cycle with the same query my application is now crashing at the declaration/assignment of the CursorLoader. The way i am understanding it is that projection is what to call the column names in the new cursor i will create and selection is the columns from which to pull out of the content provider. – spintron Nov 05 '14 at 13:16

0 Answers0