title: Getting ArrayIndexOutOfBoundsException in Adapter
body: <p>I am displaying data in a recyclerview. I am using Firebase Firestore. Here is how I am setting up my Adapter in MainActivity:  </p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      setUpRecyclerView();
  }
  
  private void setUpRecyclerView() {
      FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).build();
      db.setFirestoreSettings(settings);
  
      Query query = db.collection("users").document(firebase_user_uid).collection("notes");
  
  
      FirestoreRecyclerOptions&lt;Note&gt; response = new FirestoreRecyclerOptions.Builder&lt;Note&gt;().setQuery(query, Note.class).build();
      adapter = new NoteListAdapter(response, MainActivity.this);
  
      recyclerViewLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
      recyclerView.setLayoutManager(recyclerViewLayoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      recyclerView.addItemDecoration(new CustomRecyclerViewDivider(this, LinearLayoutManager.VERTICAL, 16));
  
      recyclerView.setAdapter(adapter);
  
      ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
      new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
  }
  </code></pre>
  
  <p>And here is my Adapter:  </p>
  
  <pre><code>public class NoteListAdapter extends FirestoreRecyclerAdapter&lt;Note, NoteListAdapter.NoteViewHolder&gt;{
  
  private Context context;
  
  public NoteListAdapter(@NonNull FirestoreRecyclerOptions&lt;Note&gt; options, @NonNull Context context) {
      super(options);
      this.context = context;
  }
  
  @Override
  protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull Note note) {
      holder.title.setText(note.getTitle());
      holder.content.setText(note.getContent());
  }
  
  @NonNull
  @Override
  public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
      return new NoteViewHolder(view);
  }
  
  public void removeItem(int position) {
      getSnapshots().getSnapshot(position).getReference().delete();
      notifyItemRemoved(position);
  }
  
  public class NoteViewHolder extends RecyclerView.ViewHolder {
  
      public TextView title, content;
      public ImageView bg_note_image;
      public RelativeLayout viewBackground, viewForeground;
  
      public NoteViewHolder(View view) {
          super(view);
          title = view.findViewById(R.id.tvTitle);
          content = view.findViewById(R.id.tvContent);
          bg_note_image = view.findViewById(R.id.note_image_view);
          viewBackground = view.findViewById(R.id.view_background);
          viewForeground = view.findViewById(R.id.view_foreground);
  
          view.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  DocumentSnapshot snapshot = getSnapshots().getSnapshot(getAdapterPosition());
                  Note note = snapshot.toObject(Note.class);
                  String id = getSnapshots().getSnapshot(getAdapterPosition()).getId();
                  MainActivity.updateNote(id, note);
                  return;
              }
          });
      }
  }}  
  </code></pre>
  
  <p>So my App crashes and thats caused by this line:  </p>
  
  <pre><code>DocumentSnapshot snapshot = getSnapshots().getSnapshot(getAdapterPosition());
  </code></pre>
  
  <p>Here's the Error:  </p>
  
  <pre><code>09-06 21:09:11.976 19959-19959/com.test.test.test E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.test.test.test, PID: 19959
  java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
      at java.util.ArrayList.get(ArrayList.java:413)
      at com.firebase.ui.common.BaseObservableSnapshotArray.getSnapshot(BaseObservableSnapshotArray.java:70)
      at com.raycroud.notes.notes.adapter.NoteListAdapter$NoteViewHolder$1.onClick(NoteListAdapter.java:97)
      at android.view.View.performClick(View.java:5685)
      at android.view.View$PerformClick.run(View.java:22481)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:241)
      at android.app.ActivityThread.main(ActivityThread.java:6274)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>
  
  <p>In the <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder#getadapterposition" rel="nofollow noreferrer">documentation</a> under getAdapterPosition() there is a Note:  </p>
  
  <pre><code>Note that if you've called notifyDataSetChanged(), until the next layout pass, the return value of this method will be NO_POSITION.  
  </code></pre>
  
  <p>So now when I don't want to crash my App I must update my OnClick in ViewHolder like this:  </p>
  
  <pre><code>@Override
              public void onClick(View view) {
                  if (getAdapterPosition() == RecyclerView.NO_POSITION) {
                      return;
                  }
                  DocumentSnapshot snapshot = getSnapshots().getSnapshot(getAdapterPosition());
                  Note note = snapshot.toObject(Note.class);
                  String id = getSnapshots().getSnapshot(getAdapterPosition()).getId();
                  MainActivity.updateNote(id, note);
                  return;
              }  
  </code></pre>
  
  <p>This code works and the App doesnt crash anymore. But when the error normally apears I can't click the Items anymore. I think that's logical right when you look at the code, because than the dapter position returns <code>-1</code> or <code>NO_POSITION</code>. So now is my Queston how to fix the whole Error? What I shuld do that the app won't crash <strong>and</strong> that I can click the Items?</p>