title: IndexOutOfBoundsException when clicking RecyclerView item
body: <p>Having problem when clicking items of RecyclerView, app crashes and giving the output:</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Invalid index 24, size is 20
          at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
          at java.util.ArrayList.get(ArrayList.java:308)
          at com.google.gson.JsonArray.get(JsonArray.java:147)
          at com.devpocket.kvartirka.MainActivity.itemClicked(MainActivity.java:334)
          at com.devpocket.kvartirka.Adapters.OffersAdapter$ViewHolder.onClick(OffersAdapter.java:161)
          at android.view.View.performClick(View.java:5162)
          at android.view.View$PerformClick.run(View.java:20873)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:145)
          at android.app.ActivityThread.main(ActivityThread.java:5834)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
  
  java.lang.IndexOutOfBoundsException: Invalid index 24, size is 20
  </code></pre>
  
  <p>The code which I'm using is like this, this is an adapter:</p>
  
  <pre><code>public class OffersAdapter extends RecyclerView.Adapter&lt;OffersAdapter.ViewHolder&gt; {
  
  private ClickListener clickListener;
  
  private CityData cityData;
  
  private ArrayList&lt;CityData&gt; cityItemList = new ArrayList&lt;CityData&gt;();
  private Context mContext;
  private String text;
  
  public static Button headerButton1;
  
  public OffersAdapter(Context context, ArrayList&lt;CityData&gt; cityItemList, String text) {
      this.cityItemList = cityItemList;
      this.mContext = context;
      this.text = text;
  }
  
  @Override
  public int getItemViewType(int position) {
  
      int viewType;
  
      if (position == 0) {
          viewType = 0;
      } else {
          viewType = 1;
      }
  
      return viewType;
  }
  
  @Override
  public OffersAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int viewType) {
  
      View itemLayoutView;
      ViewHolder viewHolder;
  
      if (viewType == 0) {
          itemLayoutView = LayoutInflater.from(viewGroup.getContext())
                  .inflate(R.layout.logo_layout, viewGroup, false);
          viewHolder = new ViewHolder(itemLayoutView, viewType);
      } else {
          itemLayoutView = LayoutInflater.from(viewGroup.getContext())
                  .inflate(R.layout.offers_singleitem, viewGroup, false);
          viewHolder = new ViewHolder(itemLayoutView, viewType);
      }
  
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(OffersAdapter.ViewHolder viewHolder, int position) {
  
      if (position &gt; 0) {
          cityData = cityItemList.get(position-1);
          String type = cityData.getType();
  
          if ("flat".equals(type)) {
              viewHolder.address.setText(cityData.getAddress());
              viewHolder.description.setText(cityData.getDescription());
              viewHolder.roomNumbers.setText(cityData.getRoomNumbers());
              if (TextUtils.isEmpty(cityData.getMetro())) {
                  viewHolder.metroImageView.setVisibility(View.GONE);
              }
              viewHolder.metro.setText(cityData.getMetro());
              viewHolder.prices.setText(cityData.getPrices() + " ₽");
              Ion.with(viewHolder.offerImage)
                      .fitXY()
                      .load(cityData.getURL());
          } else if("cottage".equals(type)) {
              viewHolder.address.setText(cityData.getAddress());
              viewHolder.description.setText(cityData.getDescription());
              viewHolder.roomNumbers.setVisibility(View.GONE);
              viewHolder.cottageImage.setVisibility(View.VISIBLE);
              if (TextUtils.isEmpty(cityData.getMetro())) {
                  viewHolder.metroImageView.setVisibility(View.GONE);
              }
              viewHolder.metro.setText(cityData.getMetro());
              viewHolder.prices.setText(cityData.getPrices() + " ₽");
              Ion.with(viewHolder.offerImage)
                      .fitXY()
                      .load(cityData.getURL());
          }
      }
  
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
      public TextView roomNumbers; //количество комнат
      public TextView address; //адресс квартиры
      public TextView description; //описание квартиры
      public TextView metro; //описание квартиры
      public TextView prices; //цена за день
      public ImageView offerImage; //картинка квартиры
      public ImageView cottageImage; //обозначение коттеджа
      public ImageView metroImageView; //значок метро
  
      public ViewHolder(View itemView, int position) {
          super(itemView);
  
  
          if(position &gt; 0) {
              roomNumbers = (TextView) itemView.findViewById(R.id.roomNumbers);
              address = (TextView)  itemView.findViewById(R.id.addressTextView);
              description = (TextView) itemView.findViewById(R.id.conditionsTextView);
              metro = (TextView) itemView.findViewById(R.id.metroTextView);
              offerImage = (ImageView) itemView.findViewById(R.id.imageView);
              prices = (TextView) itemView.findViewById(R.id.priceTV);
              cottageImage = (ImageView) itemView.findViewById(R.id.cottageImage);
              metroImageView = (ImageView) itemView.findViewById(R.id.metroImageView);
              itemView.setOnClickListener(this);
          } else {
              headerButton1 = (Button) itemView.findViewById(R.id.headerButton1);
              headerButton1.setText(Html.fromHtml(text));
              headerButton1.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent = new Intent(mContext, FilterActivity.class);
                      mContext.startActivity(intent);
                  }
              });
          }
  
  
      }
  
      @Override
      public void onClick(View v) {
  
          if(clickListener != null) {
              notifyDataSetChanged();
              clickListener.itemClicked(v, getPosition());
          }
      }
  }
  
  @Override
  public int getItemCount() {
      return (null != cityItemList ? cityItemList.size() : 0);
  }
  
  public void setClickListener(ClickListener clickListener) {
      this.clickListener = clickListener;
  }
  
  public interface ClickListener {
      public void itemClicked(View view, int position);
  }
  
  }
  </code></pre>
  
  <p>And this is how adding data into adapter:</p>
  
  <pre><code>private ArrayList&lt;CityData&gt; cityList = new ArrayList&lt;CityData&gt;();
  
  CityData cityItem = new CityData();
  cityItem.setAddress(String.valueOf(flatAddress));
  cityItem.setType(type);
  cityItem.setDescription(description);
  cityItem.setRoomNumbers(rooms);
  cityItem.setMetro(metro);
  cityItem.setUrl(imageURL);
  cityItem.setPrices(day);
  cityList.add(cityItem);
  adapter.notifyDataSetChanged();
  adapter.setClickListener(MainActivity.this);
  </code></pre>
  
  <p>And the on itemClicked </p>
  
  <pre><code>@Override
  public void itemClicked(View view, int position) {
  
      String offerData = null;
  
      for(int i = 0; i &lt;= position; i++ ) {
  
          if(i == position) {
              JsonElement obj = jsonObjectToPass.get(position-1);
              offerData = String.valueOf(obj);
          }
      }
  
      Intent intent = new Intent(MainActivity.this, OfferInfo.class);
      intent.putExtra("offerData", offerData);
      intent.putExtra("position", position);
      startActivity(intent);
  
  }
  </code></pre>