Search code examples
androidarraylistandroid-arrayadapterandroid-mediaplayeraudio-playerandroid-music-player

How does getItem() really work in android developer's sample universal music player app?


I am trying to understand the source code of the universal music player provided by android here :https://github.com/googlesamples/android-UniversalMusicPlayer

However, I don't understand the their usage of a Browser Adapter to populate the list with MediaItem. The part of the code is here:

private static class BrowseAdapter extends ArrayAdapter<MediaBrowser.MediaItem> {

        public BrowseAdapter(Activity context) {
            super(context, R.layout.media_list_item, new ArrayList<MediaBrowser.MediaItem>());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MediaBrowser.MediaItem item = getItem(position);
            System.out.println("Item: "+item);
            int itemState = MediaItemViewHolder.STATE_NONE;
            if (item.isPlayable()) {
                itemState = MediaItemViewHolder.STATE_PLAYABLE;
                MediaController controller = ((Activity) getContext()).getMediaController();
                if (controller != null && controller.getMetadata() != null) {
                    String currentPlaying = controller.getMetadata().getDescription().getMediaId();
                    String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                            item.getDescription().getMediaId());
                    if (currentPlaying != null && currentPlaying.equals(musicId)) {
                        PlaybackState pbState = controller.getPlaybackState();
                        if (pbState == null || pbState.getState() == PlaybackState.STATE_ERROR) {
                            itemState = MediaItemViewHolder.STATE_NONE;
                        } else if (pbState.getState() == PlaybackState.STATE_PLAYING) {
                            itemState = MediaItemViewHolder.STATE_PLAYING;
                        } else {
                            itemState = MediaItemViewHolder.STATE_PAUSED;
                        }
                    }
                }
            }
            return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
                item.getDescription(), itemState);
        }
    }

On the first line of getView,how does getItem() know which list/array to look for to retrieve the data at the specified 'position' ? Usually for list adapters,we pass in an array with elements already in them but in this example, they created a new ArrayList so it should be empty.So how can the getItem() retrieve from an empty ArrayList? Or does it retrieve the MediaItem from elsewhere?

Thanks.


Solution

  • The constructor for BrowseAdapter calls the parent constructor (see ArrayAdapter.java lines 154-164). The parent constructor creates a list of items (see lines 175-182).

    Items are added to the adapter which adds them to the list (MediaBrowserFragment lines 119-121). getItem(int position) then returns the item in the list (see 343-345).