title: Android RecyclerView: how to add new item to top of list?
body: <p>I have a <code>SQLite database</code> that pushes data to a <code>CardView</code> for a <code>RecyclerView</code> list.  I want newly added <code>CardViews</code> to insert at the top of the list but they are being added at the bottom, the end of the list.</p>
  
  <p>It seems like this code in the <code>MainActivity</code> is not being respected that should put the new <code>CardView</code> at the top of the list:</p>
  
  <pre><code>contactList.add(0, contact);
  </code></pre>
  
  <p>What am I missing here?</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>@Override
  protected void onStart() {
      super.onStart();
      loadData();
  }
  
  void loadData(){
      sqLiteDB = new SQLiteDB(this);
  
      List&lt;Contact&gt; contactList = new ArrayList&lt;&gt;();
  
      Cursor cursor = sqLiteDB.retrieve();
      Contact contact;
  
      // iterate over the db cursor by using a new cursor for the ArrayList.
      try {
          if (cursor.moveToFirst()) {
              while (!cursor.isAfterLast()) { // to avoid an infinite loop iteration.
                  do {
                      contact = new Contact();
                      contact.setId(cursor.getInt(0));
                      contact.setTodo(cursor.getString(1));
  
                      **contactList.add(0, contact);** // add the new item to top of R. list.
                  } while (cursor.moveToNext());
              }
          }
      } finally {
          if(cursor !=null &amp;&amp; !cursor.isClosed()){
              cursor.close();
          }
      }
  
      contactListAdapter.clear();
      contactListAdapter.addAll(contactList); 
  </code></pre>
  
  <p><strong>SQLiteDB.java</strong></p>
  
  <pre><code>public Cursor retrieve(){
      SQLiteDatabase db = getReadableDatabase();
  
      String[] projection = {
              ContactField.COLUMN_ID,
              ContactField.COLUMN_TODO
      };
  
      Cursor cursor = db.query(
              ContactField.TABLE_NAME,projection,              
              null,                    
              null,                    
              null,                    
              null,                    
              null                     
      );
  
      if (cursor == null) {
          return null;
      }
  
      return cursor;
  }
  </code></pre>
  
  <p><strong>ContactListAdapter.java</strong></p>
  
  <pre><code>public class ContactListAdapter extends RecyclerView.Adapter&lt;ContactListAdapter.ContactHolder&gt;{
  
  private List&lt;Contact&gt; contactList;
  private Context context;
  private RecyclerItemClickListener recyclerItemClickListener;
  // Setting to -1 keeps the first CardView (position 0) from having its
  // BackGroundColor mistakenly switch from the default to the highlighted/selected
  // color which is red.
  private int selectedPos = -1;
  
  public ContactListAdapter(Context context) {
      this.context = context;
      this.contactList = new ArrayList&lt;&gt;();
  }
  
  public void clear() {
      while (getItemCount() &gt; 0) {
          remove(getItem(0));
      }
  }
  
  public void addAll(List&lt;Contact&gt; contactList) {
      for (Contact contact : contactList) {
          // add(contact);
          contactList.add(0, contact);        }
  }
  
  // Get the Item's position.
  public Contact getItem(int position) {
      return contactList.get(position);
  }
  
  // Get the Item's Id.
  public long getItemId(int position) {
      return contactList.get(position).getId();
  }
  ...
  </code></pre>