0

I'm making a media player currently, and I've been searching all day long how to make a new list view that displays all the artists in the phone.

the type of list where if you click onto it, itll go to a list of the albums by that artist, then the songs etc

the extent of what I know that I have to do is use MediaStore to sort it out, but Im just stumped.

Any help? I dont even have code to go off of, cause ive been trying and deleting what Ive been doing

QConscious
  • 87
  • 1
  • 1
  • 11
  • This is off topic but still [checkout](http://code.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764) – Sanjeev Aug 24 '15 at 04:14
  • Here's a [helpful tutorial](http://www.vogella.com/tutorials/AndroidListView/article.html) that helped me a lot back in the days to understand and implement `ListView`s. – JanithaR Aug 24 '15 at 04:14
  • @L-X yeah I have that going for my main list of songs, that shows all the songs on the phone, but now I want a list only of artists, and thats where im stuck, do i make another adapter for the artist view to populate it, and if so, then whatre the proper MediaStore content code, to query the right things – QConscious Aug 24 '15 at 04:24
  • read `MediaStore.Audio.Artists` documentation – pskink Aug 24 '15 at 05:01

1 Answers1

0

I understand how it's like to have absolutely nothing to even know where to start looking.

Anyway, I use the following code for getting a list of Artists.

public ArrayList<ArtistItem> getArtistList() {
   ArrayList<ArtistItem> artistList = new ArrayList<ArtistItem>();
        Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
        String[] projection = new String[] {MediaStore.Audio.Artists._ID, MediaStore.Audio.ArtistColumns.ARTIST, MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS};
        Cursor musicCursor = getContentResolver().query(uri, projection, null, null, null);

        int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Artists._ID);
        int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST);
        int numColumn = musicCursor.getColumnIndex(MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS);
        // Iterate over the List
        if(musicCursor!=null && musicCursor.moveToFirst()) {

            //add songs to list
            do {
                String id = musicCursor.getString(idColumn);
                String title = musicCursor.getString(titleColumn);
                String num = musicCursor.getString(numColumn);
                if(title == null || title.equals(MediaStore.UNKNOWN_STRING)) {
                    title = "Unknown Artist";
                }
                if(num.equals("1")) {
                    num = num + " Song";
                } else {
                    num = num + " Songs";
                }
                artistList.add(new ArtistItem(thisId, thisTitle, thisNum));
            }  while (musicCursor.moveToNext());
        }
        return artistList;
    }

    public class ArtistItem{
        private String id;
        private String title;
        private String num;

        ArtistItem(String theId, String theTitle, String theNum) {
            id = theId;
            title = theTitle;
            num = theNum;
        }
        // TODO Implement getters and setters for id, title and num
    }
David Heisnam
  • 2,463
  • 1
  • 21
  • 32
  • Sorry to spam but if you liked my answer, could you please send me android.jar file for API level 23 to john.precursor ATGMAILDOT COM? Please? I really need it on my tablet and my laptop got fried. – David Heisnam Aug 24 '15 at 06:16
  • why do you people always iterate over the `Cursor` to get the list that is then passed to `ArrayAdapter` when you can pass it directly to `SimpleCursorAdapter`? – pskink Aug 24 '15 at 06:19
  • @pskink I prefer to customize my ListViews using a BaseAdapter. I don't think I can pass the Cursor to a BaseAdapter. Well, technically I can, but I find ArrayList easier to work with inside the BaseAdapter. – David Heisnam Aug 24 '15 at 06:21
  • i said: `"when you can pass it (a Cursor) directly to SimpleCursorAdapter"`, so just use **SimpleCursorAdapter** and dont make your life harder – pskink Aug 24 '15 at 06:23
  • @pskink Yes you can...with a SimpleCursorAdapter. But can your list items have multiple TextViews with a SimpleCursorAdapter? I need the customization, so I use BaseAdapter. And I find passing over a ArrayList to the BaseAdapter better. I don't imagine it'll be efficient to use a Cursor in a BaseAdapter. Don't you think? – David Heisnam Aug 24 '15 at 06:28
  • what `BaseAdapoter`? see `SimpleCursorAdapter` documentation, it uses a `Cursor` directly so you dont need to write simple line of code to use it (except the constructor of course) – pskink Aug 24 '15 at 06:30
  • @pskink A SimpleCursorAdapter is what the name suggestes. Simple. I usually need complex. A SimpleCursorAdapter just doesn't cut it. – David Heisnam Aug 24 '15 at 06:32
  • complex? you like your life to be harder since you have to write lots of methods to implement `BaseAdapter`? instead of just `new SimpleCursorAdapter(...)` ??? ok good luck then... did you actually read `SimpleCursorAdapter` documentation ? its Simple since its extremally simple in use.... – pskink Aug 24 '15 at 06:34
  • @pskink Yes, I just reread it now. Here's what the documentation says about what you can display with a SimpleCursorAdapter. "The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet." **When I need more than just TextViews, I use BaseAdapter** I think you have it harder relying on something so simple like a SimpleCursorAdapter. I like the freedom in using a BaseAdapter. – David Heisnam Aug 24 '15 at 06:37
  • `"First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked."` as i said good luck then in writing boilerplate code – pskink Aug 24 '15 at 06:40
  • @pskink That's nice. I guess I was wrong about the **only** TextView part. Still, I need the freedom in customization in behavior and appearance. To each his own, but I'll never use a SimpleCursorAdapter because I usually need my ListViews to more than just display things. I need BaseAdapter for **Search**, **Drag Sort**, or pretty much anything else that requires customization. – David Heisnam Aug 24 '15 at 06:46
  • and of course you dont need `BaseAdapter` for this at all, what you mean "customization" can be done with any adapter as it is the different layer of abstraction – pskink Aug 24 '15 at 06:54
  • also i have a strong feeling that you never heard of SCA before, right? try for example do the same thing (with `BaseAdapter`) like: http://stackoverflow.com/a/19860624/2252830 and compare number of lines and number of possible bugs in both solutions – pskink Aug 24 '15 at 07:00
  • @pskink "never heard of" Of course I've heard of it. It's the first adapter that I worked with when I started off with Android. Anyway, I have way too many "custom" things happening in my adapter for SCA, e.g. item selection mode, search, animate deletion, loading bitmap using AsyncTask. The OP might find SCA useful though, I admit. – David Heisnam Aug 24 '15 at 07:09
  • seems that you have limited knowledge what `Adapter` is, there is **nothing** you can do with `BaseAdapter` and you cannot do with `SCA`, you can use `ViewBinder`, you can override `newView` / `bindView` and in the extreme situation you can override any (including all) methods of `BaseAdapter`, do you see the point now? – pskink Aug 24 '15 at 07:54
  • @pskink It's rather more about need than what's possible. I don't see the point in this discussion. I'm sure my answer has served the purpose of teaching the OP how he could get Artist data from the MediaStore database. – David Heisnam Aug 24 '15 at 08:12
  • well, at least i tried and did my best to convince you to better / more efficient solutions, its a pity i failed though... – pskink Aug 24 '15 at 08:25