Retrivie VideoID



我有两个类(YtAdapter 和 MainActivity)。在YtAdapter中,我有一个获取用户输入(关键字)并搜索该关键字的Youtube视频的方法。它做得对,效果很好。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mHolder;
    if(convertView != null){
        mHolder = (ViewHolder)convertView.getTag();
    }else{
        mHolder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.view_video_item,null);
        mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
        mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
        convertView.setTag(mHolder);
    }
    //Setting the data
    SearchResult result = mVideoList.get(position);
    mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
    //Loading the image
    Picasso.with(mActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
    return  convertView;
}

唯一的问题是我可以看到视频但不能播放它们。在我的主活动中,我有一个应该播放视频的 OnItemClicked 方法,但是,我不明白如何检索列表中其中一个视频的 VideoID(字符串)。

谁能帮我完成这项工作?这是 OnItemClicked 方法的代码:

    @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    VIDEO_ID = <<<<------ I need to get the VideoID of the video im clicking on in the list of the videos.
    Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this,AppConstants.KEY,VIDEO_ID); /*Needed: result.getSnippet().get(videoID());*/
    startActivity(videoIntent);
}

我想到了YtAdpater中的一个字符串变量,并在MainActivity中使用该变量,现在正在处理它。

您可以像这样获取点击的视频 ID:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    SearchResult searchResult = (SearchResult) parent.getItemAtPosition(position);
    VIDEO_ID = searchResult.getId().getVideoId();
    Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this,AppConstants.KEY,VIDEO_ID); /*Needed: result.getSnippet().get(videoID());*/
    startActivity(videoIntent);
}

相关内容

  • 没有找到相关文章

最新更新