使用艺术家姓名填充列表视图?

时间:2015-08-24 04:08:48

标签: java android listview mediastore

我目前正在制作媒体播放器,而且我一整天都在搜索如何制作新的列表视图,以显示手机中的所有艺术家。

列表的类型,如果你点击它,它将转到该艺术家的专辑列表,然后是歌曲等

我知道我必须做的程度是使用MediaStore来解决它,但我只是难倒。

有任何帮助吗?我甚至没有代码可以离开,因为我一直在尝试并删除我正在做的事情

1 个答案:

答案 0 :(得分:0)

我明白如何知道从哪里开始寻找是绝对没有的。

无论如何,我使用以下代码获取艺术家名单。

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
    }