我想在recycleradapter中使用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")));
}
}
});
}
}
我不知道我需要改变的地方。请帮我。
答案 0 :(得分:0)
您需要将活动传递给RecyclerView适配器,然后将其更改为活动。
示例:在您的活动中(您调用recyclelerview的地方)
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));
}
希望这对你有用。