2016-02-17 4 views
3

Итак, я запускаю «проигрыватель YouTube» onClick элемента в RecyclerView. Тем не менее, здесь у меня есть проблема:Получение контекста внутри адаптера RecyclerView

Intent intent = YouTubeStandalonePlayer.createVideoIntent(itemView.getContext(), DeveloperKey.DEVELOPER_KEY, "cdgQpa1pUUE"); 
itemView.getContext().startActivity(intent); 

где itemView точка зрения, и в здесь

createVideoIntent(itemView.getContext() 

Android студия говорит:

Неправильного типа первой Argument найденного "Android. content.Context ', обязательно ' android.app.Activity '

Как это исправить? Я попробовал подобные вопросы, но решение в них было ограничено фрагментами и поддерживающими библиотеками, такими как v4 и v7. Любая помощь будет очень признательна, спасибо!

ответ

2

Try это,

Activity activity = (Activity) itemView.getContext(); 
Intent intent = YouTubeStandalonePlayer 
     .createVideoIntent(activity, DeveloperKey.DEVELOPER_KEY, "cdgQpa1pUUE"); 
itemView.getContext().startActivity(intent); 
+0

Спасибо, отредактированный один работал для меня :) – OBX

2
public class MyAdapter extends BaseAdapter { 
     private Context context; 

     public SampleAdapter(Context Mcontext) { 
      this.context = Mcontext; 
     } 

     /* ... other methods ... */ 
    } 
0

Передайте свою деятельность во Вторичный вид.

Например, как это:

public MyAdapter(MyActivity activity){ 
    this.activity = activity; 
} 

позже

YouTubeStandalonePlayer.createVideoIntent(this.activity, DeveloperKey.DEVELOPER_KEY, "cdgQpa1pUUE"); 
     itemView.getContext().startActivity(intent); 
Смежные вопросы