There are a couple of pictures with links, I want a specific video to open by clicking on them.

I process pressing as follows

viewHolder.photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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 { context.startActivity(appIntent); } catch (ActivityNotFoundException ex) { context.startActivity(webIntent); } 

Only here the whole thing in id seems to me ... how is it possible to pull out by pressing correctly if the links are different every time? It should be used for several videos, but not for one link.

adapter code

 public class ImageAdapter extends BaseAdapter { private Context context; List<ImageUpload> imageUploads; int resource; public ImageAdapter(Context context, List<ImageUpload> imageUploads, int resource) { this.context = context; this.imageUploads = imageUploads; this.resource = resource; } public View getView(final int position, View convertView, final ViewGroup parent) { final GridHolder viewHolder; if (convertView == null) { viewHolder = new GridHolder(); LayoutInflater inflater1 = LayoutInflater.from(context); if (imageUploads.get(position).getOutputMetadata().getFilename().equals("null")) { convertView = inflater1.inflate(R.layout.item_upload_default, parent, false); } else { convertView = inflater1.inflate(R.layout.item_upload, parent, false); } viewHolder.photo = (ImageView) convertView.findViewById(R.id.iv_upload); viewHolder.closeView = (ImageView) convertView.findViewById(R.id.photo_close_btn); viewHolder.progress = (ProgressWheel) convertView.findViewById(R.id.loader_photo); convertView.setTag(viewHolder); } else { viewHolder = (GridHolder) convertView.getTag(); } if (imageUploads.get(position).getOutputMetadata().getFilename().equals("null")) { viewHolder.photo.setImageResource(resource); } else { ImageUpload mobile = imageUploads.get(position); if (imageUploads.get(position).isLoadImage()) if (TextUtils.isEmpty(mobile.getOutputMetadata().getVideoUrl())) Picasso.with(context) .load(StringUtil.URLADS_THUMBLER_RESIZE + 150 + "x" + 150 + "/" + StringUtil.URLADS_THUMBLER_IMAGE + mobile.getOutputMetadata().getFilename()) .into(viewHolder.photo, new Callback() { @Override public void onSuccess() { viewHolder.progress.setVisibility(View.GONE); } @Override public void onError() { } }); else { Picasso.with(context).load(mobile.getOutputMetadata().getVideoUrl()).into(viewHolder.photo); viewHolder.progress.setVisibility(View.GONE); } else { File f = new File(imageUploads.get(position).getPath()); Picasso.with(context) .load(f) .into(viewHolder.photo); } viewHolder.closeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((GridView) parent).performItemClick(v, position, 0); } }); viewHolder.photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id = ""; String expression = ""; if (id != null && id.trim().length() > 0 && id.matches(".*\\byoutube\\b.*")) { expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; 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 { context.startActivity(appIntent); } catch (ActivityNotFoundException ex) { context.startActivity(webIntent); } } ); } return convertView; } 
  • Show the code for the entire adapter. - post_zeew
  • @post_zeew updated the question - Inkognito

1 answer 1

 ImageUpload currentImageUpload = imageUploads.get(position); 

Here currentImageUpload is the selected item. Further, according to the structure of ImageUpload you can get the contents of its specific element.


UPD .

 viewHolder.photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id = imageUploads.get(position).getId(); 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 { context.startActivity(appIntent); } catch (ActivityNotFoundException ex) { context.startActivity(webIntent); } } } 

Here you need to replace the getId() method with the method that returns the Id your object of type ImageUpload .

  • Thank you, but it's still not very clear how it can be used in the onClick method for the "selected item". Namely getting the content from the item. - Inkognito
  • Add the code of the ImageUpload class and indicate that there is a “link” in it that you want to open. - post_zeew
  • emphasizes position ... - Inkognito
  • And what does he say? Well, I hope you want to use this position where it is declared, right? Where? - post_zeew
  • In the getView method, inserted everything works fine. And where can you now indicate that there is a link in it that I want to open? - Inkognito