android - Youtubestandaloneplayer在recycleradapter错误中

标签 android youtube android-recyclerview youtube-api android-youtube-api

我想在我的回收站中使用Youtubestandaloneplayer,但是“this”中有错误,

if (pos == 0) {
    context.startActivity(YouTubeStandalonePlayer.createVideoIntent(this, DEVELOPER_KEY, VIDEO_ID));
}

这是我的代码:
class ViewHolder extends RecyclerView.ViewHolder {

    private Context context;

    public int currentItem;
    public ImageView itemImage;
    public TextView itemTitle;
    public TextView itemDetail;

    public ViewHolder(final View itemView) {
        super(itemView);
        context = itemView.getContext();
        itemImage = (ImageView) itemView.findViewById(R.id.item_image);
        itemTitle = (TextView) itemView.findViewById(R.id.item_title);
        itemDetail = (TextView) itemView.findViewById(R.id.item_detail);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = getAdapterPosition();
                if (pos == 0) {
                    context.startActivity(YouTubeStandalonePlayer.createVideoIntent(this, DEVELOPER_KEY, VIDEO_ID));
                } else if (pos == 1) {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.naver.com")));
                } else if (pos == 2) {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.daum.net")));
                }
            }
        });
    }
}

我不知道我该在哪里改变。请帮我。

最佳答案

您需要将 Activity 传递到RecyclerView适配器,然后将其更改为 Activity 。
示例:在您的 Activity 中(在哪里调用recyclerview)adapter = new ExampleRecyclerAdapter(CurrentActivity.this, data);
在您的RecyclerView适配器中:
Activity activity; ExampleRecyclerAdapter(CurrentActivity activity, Data data) { this.activity = activity; }
然后将您的代码this替换为activity

context.startActivity(YouTubeStandalonePlayer.createVideoIntent(activity, DEVELOPER_KEY, VIDEO_ID));

}

希望这可能对您有用。

关于android - Youtubestandaloneplayer在recycleradapter错误中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200621/

相关文章:

android - 如何停止 MediaStore.ACTION_IMAGE_CAPTURE 复制图片

java - 使用 GSON 的空字段的默认值

youtube - 通过 Youtube Api 上传 360 度全景视频

ios - 在youtube-ios-player-helper中设置 “origin”会破坏编程功能

android - SwitchCompat 在 RecyclerView 中给出空指针异常

java - 任务 ':app:transformClassesWithDexForDebug' 执行失败 dex 进程返回代码 1

java - 在 auth 库中找不到类 GoogleSignIn

php - 如何使直接 youtube 视频 url 工作不嵌入?

android - 如何处理 RecyclerView.ItemDecoration 中的点击事件?

android - 使用具有不同布局的相同 recyclerview 适配器