I am using the Youtube API to play Videos in a recyclerView
But i have to HARDCODE the videoID everytime, also I have created a list of videoID, how to pass the respective videoID instead of hardcoding.
Here is my VideoActivity.java :
RecyclerView recyclerView;
List<com.devapps.masjid.CustomModel> myModelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myModelList = new ArrayList<>();
myModelList.add(new com.devapps.masjid.CustomModel("hMy5za-m5Ew"));
myModelList.add(new com.devapps.masjid.CustomModel("unU9vpLjHRk"));
myModelList.add(new com.devapps.masjid.CustomModel("k9zTr2MAFRg"));
recyclerView.setAdapter(new CustomAdapter(this,myModelList));
Here is MyAdapter Class :
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
Context context;
List<com.devapps.masjid.CustomModel> myModelList;
public CustomAdapter(Context context, List<com.devapps.masjid.CustomModel> myModelList) {
this.context = context;
this.myModelList = myModelList;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final com.devapps.masjid.CustomModel myModel = myModelList.get(position);
final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
Toast.makeText(context, "Some Error Occured !", Toast.LENGTH_SHORT).show();
Log.e("Error : " , String.valueOf(errorReason));
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.show();
youTubeThumbnailView.setVisibility(View.VISIBLE);
holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
progressDialog.hide();
}
};
holder.youTubeThumbnailView.initialize(YoutubeConfig.getApiKey(), new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(myModel.getId());
youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(context, "Some Error Occured OnIntitialisationFailure!", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Details : " + youTubeInitializationResult, Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return myModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
YouTubeThumbnailView youTubeThumbnailView;
protected ImageView playButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
playButton = (ImageView) itemView.findViewById(R.id.btnYoutube_player);
playButton.setOnClickListener(this);
relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
}
@Override
public void onClick(View view) {
Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) context, YoutubeConfig.getApiKey(), "unU9vpLjHRk", 100,
true,
true);
context.startActivity(intent);
}
}
}
Here is my Model Class :
public class CustomModel {
String id;
public CustomModel(){
}
public CustomModel(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
How to pass the Current VideoId from the list instead of hardcoding a single value as you can see in OnClick() in Adapter Class ?