There is a list with preview's videos from youtube.

void getIdUrl(String url, int position) { String video_id = ""; String expression = ""; if (url != null && url.trim().length() > 0 && url.matches(".*\\byoutube\\b.*")) { expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; CharSequence input = url; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { String groupIndex1 = matcher.group(7); if (groupIndex1 != null && groupIndex1.length() == 11) video_id = groupIndex1; ImageUpload imageUpload = new ImageUpload(); imageUpload.setLoadImage(true); OutputMetadata outputMetadata = new OutputMetadata(); outputMetadata.setVideoUrl("http://img.youtube.com/vi/" + video_id + "/0.jpg"); outputMetadata.setFilename("http://img.youtube.com/vi/" + video_id + "/0.jpg"); imageUpload.setOutputMetadata(outputMetadata); controlList.get(position).getImageUploads().add(0,imageUpload); adapterForm.notifyDataSetChanged(); } 

The task is to click on an item in the list to open the selected youtube video .

I tried directly in the adapter implement clicking on the picture as follows:

 viewHolder.photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=Hxy8BZGQ5Jo"))); Log.i("Video", "Video Playing...."); } }); 

Perhaps a method and a worker, but allocates startActivity red.

Also found something similar:

  public static void watchYoutubeVideo(String id){ Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id)); Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + id)); try { startActivity(appIntent); } catch (ActivityNotFoundException ex) { startActivity(webIntent); } } 

But I can't insert it correctly into my getIdUrl method.

    1 answer 1

    Add the Context adapter constructor:

     private Context mContext; ... public MyAdapter(Context context) { mContext = context; } 

    And further:

     mContext.startActivity(...); 
    • Thanks for the reaction, and if the video is not only from youtube, just use the onClick switch method, substituting the individual Uri.parse? - Morozov
    • Yes, something like that. - post_zeew 1:58 pm
    • Is it possible to write my getIdUrl method in onClick? To he immediately and check links? - Morozov
    • To answer this question, you need to know the context of the problem. If I understand correctly, then your getIdUrl(...) method checks the url and adds the video preview to the list. So, do you want to check something again when clicking on the preview? But why? You have already checked when adding a preview. - post_zeew
    • Because at the moment in my onClick method, the default link is {startActivity (new Intent (Intent.ACTION_VIEW, Uri.parse (" youtube.com/watch?v=Hxy8BZGQ5Jo" )))}; and I need to open it videos from the list that are already uploaded by the user. Simply speaking, by clicking to open the videos inserted into the list (the list is different each time). And do not open the same video, regardless of the selected item. - Morozov