title: I need a way around to tackle the following IndexOutOfBoundsException
body: <p>I have a list. I set cardview card for each item of the list.I have implemented remove on swipe up , but when I remove last item(last card) an IndexOutOfBoundsException is thrown. 
  My code:</p>
  
  <p>Activity:</p>
  
  <pre><code>public class FirstPage extends Activity
  {
      RallyRestApi restApi;
      private RecyclerView recyclerView;
      private CustomAdapter adapter;
      private List&lt;MyData&gt; data_list;
      private Context mcontext;
      String username;
      String password;
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.activity_first_page);
          mcontext=this;
          recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
          data_list = new ArrayList&lt;&gt;();
          load_data();
          recyclerView.setLayoutManager(new LinearLayoutManager(mcontext,LinearLayoutManager.HORIZONTAL,false));
          adapter = new CustomAdapter(FirstPage.this,data_list);
          recyclerView.setAdapter(adapter);
          username=getIntent().getStringExtra("username");
          password=getIntent().getStringExtra("password");
          ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.UP )
          {
              @Override
              public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
              {
                  Toast.makeText(getApplicationContext(), "on Move", Toast.LENGTH_SHORT).show();
                  return false;
              }
  
              @Override
              public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir)
              {
  
                  Toast.makeText(getApplicationContext(), "Task Status changed to COMPLETE", Toast.LENGTH_LONG).show();
                  String username = getIntent().getStringExtra("username");
                  String password = getIntent().getStringExtra("password");
                  try
                  {
                      restApi=new RallyRestApi(new URI("https://rally1.rallydev.com"),username,password);
                      JsonObject updatedValues = new JsonObject();
                      updatedValues.addProperty("State", "Completed");
                      UpdateRequest taskUpdate = new UpdateRequest(data_list.get(viewHolder.getAdapterPosition()).getRef(), updatedValues);
                      restApi.update(taskUpdate);
                      data_list.remove(viewHolder.getAdapterPosition());
                      adapter.notifyDataSetChanged();
                  }
                  catch (URISyntaxException | IOException e)
                  {
                              e.printStackTrace();
                  }
              }
          };
          ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
          itemTouchHelper.attachToRecyclerView(recyclerView);
      }
  </code></pre>
  
  <p>my Adapter class:</p>
  
  <pre><code>public class CustomAdapter extends RecyclerView.Adapter&lt;CustomAdapter.ViewHolder&gt; {
  
      private Context context;
      private List&lt;MyData&gt; my_data;
      FirstPage activity;
      RallyRestApi restApi;
  
      public CustomAdapter(FirstPage activity, List&lt;MyData&gt; my_data)
      {
  
          this.my_data = my_data;
          this.activity=activity;
  
      }
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
      {
          View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
          itemView.setMinimumWidth(parent.getMeasuredWidth());
          itemView.setMinimumHeight(parent.getMeasuredHeightAndState());
          return new ViewHolder(itemView);
      }
  
      @Override
      public void onBindViewHolder(final ViewHolder holder, final int position)
      {
          holder.userstory.setText("User Story: "+my_data.get(position).getUserstory());
          holder.tasks.setText("Task: "+my_data.get(position).getTask());
          holder.seekBar.setMax(my_data.get(position).getEstimate());
          holder.seekBar.setProgress(my_data.get(position).getActual());
          holder.actual_estimate.setText(my_data.get(position).getActual()+"/"+my_data.get(position).getEstimate());
          holder.seekBar.setOnSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {
              @Override
              public void onProgressChanged(CircularSeekBar circularSeekBar, int progress, boolean fromUser)
              {
  //IOB exception
                  holder.seekBar.setProgress(holder.seekBar.getProgress());
                  holder.actual_estimate.setText(holder.seekBar.getProgress()+"/"+my_data.get(position).getEstimate());
              }
  
              @Override
              public void onStopTrackingTouch(CircularSeekBar seekBar)
              {
                  try
                  {
                      restApi=new RallyRestApi(new URI("https://rally1.rallydev.com"),activity.username,activity.password);
                      JsonObject updatedValues = new JsonObject();
                      updatedValues.addProperty("Actuals", holder.seekBar.getProgress());
                      UpdateRequest taskUpdate = new UpdateRequest(my_data.get(position).getRef(), updatedValues);
                      restApi.update(taskUpdate);
                      holder.actual_estimate.setText(holder.seekBar.getProgress()+"/"+my_data.get(position).getEstimate());
                      holder.seekBar.setProgress(holder.seekBar.getProgress());
                  }
                  catch (URISyntaxException | IOException e)
                  {
                      e.printStackTrace();
                  }
              }
              @Override
              public void onStartTrackingTouch(CircularSeekBar seekBar)
              {
  
              }
          });
          holder.seekBar.setProgress(holder.seekBar.getProgress());
          holder.actual_estimate.setText(holder.seekBar.getProgress()+"/"+my_data.get(position).getEstimate());
      }
  
      @Override
      public int getItemCount()
      {
          return my_data.size();
      }
  
      public  class ViewHolder extends  RecyclerView.ViewHolder
      {
          public TextView userstory,tasks,actual_estimate;
          public CircularSeekBar seekBar;
  //
          public ViewHolder(View itemView)
          {
              super(itemView);
              userstory=(TextView) itemView.findViewById(R.id.tvUserStory);
              tasks=(TextView) itemView.findViewById(R.id.tvTask);
              seekBar=(CircularSeekBar) itemView.findViewById(R.id.circularSeekBar1);
              actual_estimate=(TextView) itemView.findViewById(R.id.tvactuals_estimate);
          }
      }
  }
  </code></pre>
  
  <p>ErrorLog:</p>
  
  <blockquote>
    <p>java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
                                                                                          at
    java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                          at java.util.ArrayList.get(ArrayList.java:308)
                                                                                          at
    com.bmc.apetkar.akshay_rallyrest.CustomAdapter$1.onProgressChanged(CustomAdapter.java:68)
                                                                                          at
    com.circularseekbar.CircularSeekBar.setProgress(CircularSeekBar.java:530)
                                                                                          at
    com.bmc.apetkar.akshay_rallyrest.CustomAdapter$1.onProgressChanged(CustomAdapter.java:67)
                                                                                          at
    com.circularseekbar.CircularSeekBar.setProgress(CircularSeekBar.java:530)
                                                                                          at
    com.bmc.apetkar.akshay_rallyrest.CustomAdapter.onBindViewHolder(CustomAdapter.java:61)
                                                                                          at
    com.bmc.apetkar.akshay_rallyrest.CustomAdapter.onBindViewHolder(CustomAdapter.java:32)
                                                                                          at
    android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
                                                                                          at
    android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
                                                                                          at
    android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
                                                                                          at
    android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
                                                                                          at
    android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
                                                                                          at
    android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
                                                                                          at
    android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
                                                                                          at
    android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
                                                                                          at
    android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
                                                                                          at
    android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
                                                                                          at android.view.View.layout(View.java:16630)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                                          at android.view.View.layout(View.java:16630)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                                          at android.view.View.layout(View.java:16630)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                                          at android.view.View.layout(View.java:16630)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                                          at
    com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                                          at android.view.View.layout(View.java:16630)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                                          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                                          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>
  
  <p>I know it might be a duplicate of IndexOutOfBoundsException but I referred it and did not understand where exactly I need to change to <code>position-1</code> as list starts from 0;</p>