title: App crash on last item deleted in RecyclerView
body: <p>I'm using a RecyclerView and populating data from the database and storing it into arrayLists. i've also added an onLongClickListener() to the recycler view which removes the long clicked row. Everything works fine but when I delete the last the app crashes and i get Java.Lang.IndexOutOfBoundsException index:0 size:0.</p>
  
  <p>here's my code</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements AlarmRecyclerViewInterface {
  
      FloatingActionButton mAlarmAddButton;
      RecyclerView mRecyclerView;
      SQLiteDatabase db;
      AlarmsDBhelperClass mAlarmsDBhelperClass;
      ArrayList&lt;String&gt; nameArrayList = new ArrayList&lt;&gt;();
      ArrayList &lt;String&gt; modeArrayList = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; repeatArrayList = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; hoursArrayList = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; minArrayList = new ArrayList&lt;&gt;();
      Switch mSwitch;
      AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
      ImageView emptyImageView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
          //Initializing RecyclerView, DatabaseHelperClass, FAB button, The ON OFF switch &amp; the empty ImageView
          mAlarmsDBhelperClass = new AlarmsDBhelperClass(getApplicationContext());
          mAlarmAddButton = findViewById(R.id.btnAlarmADD);
          mRecyclerView = findViewById(R.id.alarmList);
          mSwitch = findViewById(R.id.switchONOFF);
          emptyImageView = findViewById(R.id.empty_view);
  
          //DividerItemDecoration class is used for getting a vertical line between rows of RecyclerView
          DividerItemDecoration itemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
          mRecyclerView.addItemDecoration(itemDecoration);
  
          //Getting a writable reference of the Database.
          db = mAlarmsDBhelperClass.getWritableDatabase();
  
          //Retrieving values from the database and storing them in custom ArrayLists
          boolean isDataEmpty = getAlarm(db);
  
          //Checking if our arrayList is empty? if yes then display some empty list text or an image
          if (!isDataEmpty){
              mRecyclerView.setVisibility(View.GONE);
              emptyImageView.setImageResource(R.drawable.no_alarm_black_white);
              emptyImageView.setVisibility(View.VISIBLE);
          }
          else {
              mRecyclerView.setVisibility(View.VISIBLE);
              emptyImageView.setVisibility(View.GONE);
          }
  
          //FAB Event handling
          mAlarmAddButton.setImageResource(R.drawable.addalarm);
          mAlarmAddButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent mIntent = new Intent(getApplicationContext(), AddAlarm.class);
                  startActivity(mIntent);
              }
          });
  
         //Warping up with the recyclerView
          mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
          mRecyclerView.setAdapter(alarmAdapter);
          mRecyclerView.setHasFixedSize(true);
      }
  
      public boolean getAlarm(SQLiteDatabase db) {
          Cursor cursor = db.rawQuery("SELECT * FROM alarms", new String[]{});
          boolean rowExists;
          if (cursor.moveToFirst()) {
              do {
                  nameArrayList.add(cursor.getString(2));
                  modeArrayList.add(cursor.getString(3));
                  repeatArrayList.add(cursor.getString(4));
                  hoursArrayList.add(Integer.toString(cursor.getInt(5)));
                  minArrayList.add(Integer.toString(cursor.getInt(6)));
                  rowExists = true;
              } while (cursor.moveToNext());
          }else {
              rowExists = false;
          }
          cursor.close();
          return rowExists;
      }
  
      //RecyclerView's onClick()
      @Override
      public void onItemClick(int position) {
          Toast.makeText(this, "Alarm Clicked !", Toast.LENGTH_SHORT).show();
      }
  
      //RecyclerView's onLongClick()
      @Override
      public void onLongItemClick(int position) {
          hoursArrayList.remove(position);
          //Updating the recyclerView
          alarmAdapter.notifyItemRemoved(position);
          //Deleting the row from the database
          db.delete("alarms","hours=?",new String[]{hoursArrayList.get(position)});
          Toast.makeText(this,"Alarm Deleted !", Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>here's my RecyclerView Adapter</p>
  
  <pre><code>public class AlarmAdapter extends RecyclerView.Adapter&lt;AlarmAdapter.AlarmView&gt; {
      //Variables for the main recycler view
      private ArrayList&lt;String&gt; hoursArrayList;
      private ArrayList&lt;String&gt; minArrayList;
      private ArrayList&lt;String&gt; modeArrayList;
      private ArrayList&lt;String&gt; repeatArrayList;
      private ArrayList&lt;String&gt; nameArrayList;
      private AlarmRecyclerViewInterface mInterface;
  
      public AlarmAdapter(ArrayList&lt;String&gt; hours,ArrayList&lt;String&gt; mins,ArrayList&lt;String&gt; mode,ArrayList&lt;String&gt; repeat,ArrayList&lt;String&gt; name,AlarmRecyclerViewInterface mInterface){
          this.hoursArrayList = hours;
          this.minArrayList = mins;
          this.modeArrayList = mode;
          this.nameArrayList = name;
          this.repeatArrayList = repeat;
          this.mInterface = mInterface;
      }
  
      @NonNull
      @Override
      public AlarmView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
          View view = layoutInflater.inflate(R.layout.alarm_profile,parent,false);
          return new AlarmView(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull AlarmView holder, int position) {
          if(Integer.parseInt(hoursArrayList.get(position)) &lt; 10 ){
              holder.hours.setText("0"+hoursArrayList.get(position));
          }else {
              holder.hours.setText(hoursArrayList.get(position));
          }
          if (Integer.parseInt(minArrayList.get(position)) &lt; 10){
              holder.mins.setText("0"+minArrayList.get(position));
          }else {
              holder.mins.setText(minArrayList.get(position));
          }
          holder.repeat.setText(repeatArrayList.get(position));
          holder.mode.setText(modeArrayList.get(position));
          holder.name.setText(nameArrayList.get(position));
      }
  
      @Override
      public int getItemCount() {
          return hoursArrayList.size();
      }
  
      /*ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
          @Override
          public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
              return false;
          }
  
          @Override
          public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
              nameArrayList.remove(viewHolder.getAdapterPosition());
              modeArrayList.remove(viewHolder.getAdapterPosition());
              repeatArrayList.remove(viewHolder.getAdapterPosition());
              hoursArrayList.remove(viewHolder.getAdapterPosition());
              minArrayList.remove(viewHolder.getAdapterPosition());
          }
      };*/
  
      public class AlarmView extends RecyclerView.ViewHolder{
          TextView hours,mins,repeat,name,mode;
          public AlarmView(@NonNull View itemView) {
              super(itemView);
              hours = itemView.findViewById(R.id.txtHOUR);
              mins = itemView.findViewById(R.id.txtMins);
              repeat = itemView.findViewById(R.id.txtRepeatDays);
              name = itemView.findViewById(R.id.txtName);
              mode = itemView.findViewById(R.id.txtMode);
  
              itemView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      mInterface.onItemClick(getAdapterPosition());
                  }
              });
  
              itemView.setOnLongClickListener(new View.OnLongClickListener() {
                  @Override
                  public boolean onLongClick(View view) {
                      mInterface.onLongItemClick(getAdapterPosition());
                      return true;
                  }
              });
  
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks for the help!</p>