There is a method that compares the links of one object with another:

public boolean contains(ImageUpload newVideoUpload) { for (ImageUpload imageUpload : imageUploads) { if (imageUpload.getUrl().equals(newVideoUpload.getUrl())) return true; } return false; } 

And the method in which I work with youtube id’s (pull out id, after which I insert a preview video where I display it on the screen):

 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(imageUpload); adapterForm.notifyDataSetChanged(); } else { Toast.makeText(getActivity().getApplicationContext(), "This video already exists!", Toast.LENGTH_SHORT).show(); } 

The question is how to use the contains method in the getIdUrl method.

  • one
    In this form, you will not be able to use it, because the type of the object, which, in particular, contains a link to the preview, you have ImageUpload , and in the contains method - VideoUpload . Bring them to the same denominator. - post_zeew
  • And a small note, judging by the name of the getIdUrl(...) method, it should return something. - post_zeew
  • сontains put in order (updated question). What about the remark did not quite understand what he should return. - Morozov
  • If you check the presence of an element by comparing links to complete videos, then it is better to do this at the very beginning of the method, so as not to make unnecessary manipulations if the object is already in the list. By the remark, if get present in the method name, then, logically, this method is used to get something, that is, it must return something. If it does not return anything to you, then you should think about the correct name of the method in accordance with its functionality. - post_zeew

1 answer 1

Alternatively, you can change the contains(...) method:

 public boolean contains(String url) { for (ImageUpload imageUpload : imageUploads) { if (imageUpload.getUrl().equals(url)) return true; } return false; } 

And in the getIdUrl(...) method, first check for the presence of an object:

 void getIdUrl(String url, int position) { if (ClassName.contains(url)) { ... } else { ... } } 

where ClassName is the name of the class where the contains(...) method is defined.

  • The contains method is located in the same fragment. + if I understand correctly, instead of ... after if (ClassName.contains (url)) {, will we add another three more if? - Morozov
  • In the block after if(...) should add the code that will be executed if the video being added is already in the list . In the block after else you need to add code that will be executed if the video being added is not in the list yet . - post_zeew