ArrayList declaration in method

viewHolder.photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<String> images = new ArrayList<>(); Intent intent = new Intent(context, FullScreenViewActivity.class); intent.putExtra("position", position); images.add(0, "https://" + StringUtil.URLADS_THUMBLER_IMAGE + images.get(position)); intent.putStringArrayListExtra("items_to_parse", (ArrayList<String>) images); context.startActivity(intent); } }); 

At the moment I tried to add a link to the image in this way.

 images.add(0, "https://" + StringUtil.URLADS_THUMBLER_IMAGE + images.get(position)); 

But knocks error:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

But I need to add several links , and they are not fixed - each time they are different (they are entered by users).

  • can you explain more clearly what you want? - Maxim Kuznetsov
  • @MaksimKuznetsov, in my opinion, the name of the question speaks for itself, isn't it? - Inkognito
  • private void yourMethod () {List <String> images = new ArrayList <> ();} method declaration - Maxim Kuznetsov
  • 3
    The essence of the issue is not clear. - Roxio0
  • You announced the list and immediately took the string from him, so the error is Maxim Kuznetsov.

1 answer 1

Try this:

 intent.putStringArrayListExtra("items_to_parse", new ArrayList<>()); 

You can set the size or fill immediately, for example:

 intent.putStringArrayListExtra("items_to_parse", new ArrayList<>(Arrays.asList("Первый", "Второй", "Третий"))); 
  • Thanks, but my size can be every time. It depends on how many users add photos to the list. - Inkognito