Android java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
<p>I have a ToDo App and I need to sort two ArrayLists - first with Strings(entry name given by the user) and second with Bitmaps(image added by the user as an attachment to the entry). If I'll sort ArrayList with Strings I have to sort an array with Bitmaps too, because Bitmaps have to match the Strings.
Sorting of the first arraylist(Strings) works just fine but with the second arraylist(Bitmasp) don't. I'm getting this error:</p>
<blockquote>
<p>FATAL EXCEPTION: main
Process: com.example.administrator.myapplication, PID: 23690
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.administrator.myapplication.CustomAdapter.getView(CustomAdapter.java:67)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1132)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)</p>
</blockquote>
<pre><code>private void Sort(String param){


    Toast t2 =  Toast.makeText(MainActivity.this, &quot;You selected : &quot; + param, Toast.LENGTH_LONG);
   // t2.show();
    String t;

    items2.clear();




    switch(param) {

        case &quot;Nazwa&quot;:
            Collections.sort(events, Zdarzenie.SortNazwa);
            break;

        case &quot;Data dodania&quot;:
            Collections.sort(events, Zdarzenie.SortDataDod);
            break;

        case &quot;Data zakończenia&quot;:
            Collections.sort(events, Zdarzenie.SortData);
            break;

        case &quot;Priorytet&quot;:
            Collections.sort(events, Zdarzenie.SortPriorytet);
            break;
        case &quot;Status&quot;:
            Collections.sort(events, Zdarzenie.SortStatus);
            break;

    }
    for (int i = 0; i &lt; events.size(); i++) {
        Zdarzenie z = events.get(i);

          if(param.equalsIgnoreCase(&quot;Nazwa&quot;)){
              t = z.getNazwa() + &quot; &quot; + z.getGodzina() + &quot; &quot; + z.getData() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
              items2.add(t);
          }
         if(param.equalsIgnoreCase(&quot;Data dodania&quot;)){
             t = z.getData_dodania() + &quot; &quot; + z.getGodzina_dodania() + &quot; &quot; + z.getNazwa() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
             items2.add(t);
         }
        if(param.equalsIgnoreCase(&quot;Data zakończenia&quot;)){
            t = z.getData() + &quot; &quot; + z.getGodzina() + &quot; &quot; + z.getNazwa() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
            items2.add(t);
        }
        if(param.equalsIgnoreCase(&quot;Priorytet&quot;)){
            t = z.getPriorytet() + &quot; &quot; + z.getNazwa() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
            items2.add(t);
        }
        if(param.equalsIgnoreCase(&quot;Status&quot;)){
            if (z.isAktywny() == true) {
                t = &quot;Aktywny&quot; + &quot; &quot; + z.getNazwa() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
                items2.add(t);
            }
            if (z.isAktywny() == false) {
                t = &quot;Nieaktywny&quot; + &quot; &quot; + z.getNazwa() + &quot; &quot; + z.getData() + &quot; &quot; + z.getGodzina();
                items2.add(t);
            }

        }


    }

    //itemsAdapter2 = new CustomAdapter(this, items2,images2);



    int is = itemsAdapter.getCount();

    Toast t3 =  Toast.makeText(MainActivity.this, &quot;Items2.size : &quot; + is, Toast.LENGTH_LONG);
    t3.show();

           String tmp1,tmp2;

    for(int i =0 ; i&lt;is ; i++){

        for(int j=0; j&lt;is; j++){

            itemsAdapter.getItem(i);
         //   images.get(i);
           // itemsAdapter2.getItem(j);
            items2.get(j);
           // if(itemsAdapter.getItem(i).equals(itemsAdapter2.getItem(j))){

               if( itemsAdapter.getItem(i).toString().contains(items2.get(j).toString())){


                for(int k = 0 ; k&lt;is; k++){
                    if ( k == j)

                    images2.add(k,itemsAdapter.image.get(i));
                    //itemsAdapter.image.get(i);
                }
            }



        }
    }
    // lvItems.setAdapter(new CustomAdapter(this, items2,images));
       /* itemsAdapter2 = new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_list_item_1, items2);*/
    itemsAdapter2 = new CustomAdapter(this, items2,images2);
   // itemsAdapter2.notifyDataSetChanged();
    lvItems.setAdapter(itemsAdapter2);

}
</code></pre>
<p>CustomAdapter.java</p>
<pre><code>    package com.example.administrator.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter{
   public static View rowView;
    ArrayList&lt;String&gt; result;
    Context context;
    //Bitmap [] image;
    ArrayList&lt;Bitmap&gt; image;
    private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, ArrayList&lt;String&gt; items, ArrayList&lt;Bitmap&gt; images) {
        // TODO Auto-generated constructor stub
        result=items;
        context=mainActivity;
        image=images;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();

        rowView = inflater.inflate(R.layout.list_single, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
        holder.tv.setText(result.get(position));
       // holder.img.setImageResource(image.get(position));
        holder.img.setImageBitmap(image.get(position));
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, &quot;You Clicked &quot;+position, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context.getApplicationContext() ,EditActivity.class);
                String message = Integer.toString(position);
                //long pos = listView.getItemIdAtPosition(position);
               intent.putExtra(&quot;message&quot;, message);
                context.startActivity(intent);`enter code here`
            }
        });
        return rowView;
    }

}
</code></pre>