Search code examples
androidandroid-youtube-api

Android how to show ListView of YouTubeThumbnail


I am using YouTube Android Player API to play videos in my application. I am trying to show thumbnails of the videos and when somebody clicks on the thumbnail, the video will be played.

The main activity has a list view, i am trying to add thumbnails of videos to this listview.

I created custom adapter for this,

        public class MySimpleArrayAdapter extends ArrayAdapter<String> implements             YouTubeThumbnailView.OnInitializedListener {
        private final Context context;
        private final ArrayList<String> values;
        int videoPosition=0;
        private YouTubeThumbnailLoader youTubeThumbnailLoader;
        String API_KEY="MYKEY";

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.thumbnail, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("ViewHolder") View rowView = inflater.inflate(R.layout.thumbnail, parent, false);
        videoPosition=position;
        YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView)rowView.findViewById(R.id.thumb);
        youTubeThumbnailView.initialize(API_KEY, this);

        return rowView;
    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader thumbnailLoader) {
        youTubeThumbnailLoader=thumbnailLoader;
        thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
        youTubeThumbnailLoader.setVideo(values.get(videoPosition));;

    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {    
}


    private final class ThumbnailLoadedListener implements
            YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, YouTubeThumbnailLoader.ErrorReason arg1) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {

        }

    }
}

And i am setting this adapter to the listview and i am passing three video ids(as a ArrayList) to this adapter, but the problem with this is whenever the line youTubeThumbnailLoader.setVideo(values.get(videoPosition)) is executed, the variable videoPosition is 2 and it is setting the same thumbnail for all the videos.

I thought may be the adapter is not waiting till the thumbnail is loaded and quickly rendering all the rows(because the value of videoPosition is reaching to the final value) i tried adding 'while(isWait);' before return rowview (i am setting isWait to true in onThumbnailLoaded and, onThumbnailError functions and isWait is a AtomicBoolean ) but it did not work.

Can somebody tell me why this is happening and how to solve this??

Thank you :)


Solution

  • In this line youTubeThumbnailView.initialize(API_KEY, this); you are setting this to implement the YouTubeThumbnailView.OnInitializedListener.

    Instead change the line to this

    youTubeThumbnailView.initialize(API_KEY, new YouTubeThumbnailView.OnInitializedListener(){
    
    //override methods here..
    
    });
    

    You might need to declar videoPosition as final.

    Hope this workss...