title: Android FireBase change Listview order
body: <p>My application is returning the latest data from firebase to the buttom of the ListView. But I want it to be on the top! I have thought about it and I think there is only two possible ways to do it.</p>
  
  <p><strong>1.</strong> <strong>Invert the Listview.</strong>
  <br>
  I think that this way is how it should be done but I couldn't figure it out. I have searched a lot on the web but no suitable solution for my case 
  <br>
  This is my adapter code
  <br></p>
  
  <pre><code> public void onStart() {
          super.onStart();
          // Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
          final ListView listView = getListView();
          // Tell our list adapter that we only want 50 messages at a time
          mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, mUsername);
          listView.setAdapter(mChatListAdapter);
  }
  </code></pre>
  
  <p>And this is the code for the <code>ChatListAdapter</code> constructor for a custom list class <code>ChatListAdapter</code> which extends special list adapter class <code>FirebaseListAdapter</code>:</p>
  
  <pre><code>public ChatListAdapter(Query ref, Activity activity, int layout, String mUsername) {
      super(ref, Chat.class, layout, activity);
      this.mUsername = mUsername;
  }
  </code></pre>
  
  <p><strong>[Edit]</strong> This is some of the code for <code>FirebaseListAdapter</code> which extends <code>BaseAdapter</code> class</p>
  
  <pre><code> public FirebaseListAdapter(Query mRef, Class&lt;T&gt; mModelClass, int mLayout, Activity activity) {
      this.mRef = mRef;
      this.mModelClass = mModelClass;
      this.mLayout = mLayout;
      mInflater = activity.getLayoutInflater();
      mModels = new ArrayList&lt;T&gt;();
      mModelKeys = new HashMap&lt;String, T&gt;();
      // Look for all child events. We will then map them to our own internal ArrayList, which backs ListView
      mListener = this.mRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
  
              T model = dataSnapshot.getValue(FirebaseListAdapter.this.mModelClass);
              mModelKeys.put(dataSnapshot.getKey(), model);
  
              // Insert into the correct location, based on previousChildName
              if (previousChildName == null) {
                  mModels.add(0, model);
              } else {
                  T previousModel = mModelKeys.get(previousChildName);
                  int previousIndex = mModels.indexOf(previousModel);
                  int nextIndex = previousIndex + 1;
                  if (nextIndex == mModels.size()) {
                      mModels.add(model);
                  } else {
                      mModels.add(nextIndex, model);
                  }
              }
          }
  </code></pre>
  
  <p><strong>2. Descending query the data.</strong>
  <br>
  The second way seams impossible to me, because when I searched on Firebase API documentation and on the web, I couldn't find anyway to order retraived data on descending way.
  <br>
  My data on firebase look like the following:</p>
  
  <pre><code>glaring-fire-9714 
      chat
          -Jdo7-l9_KBUjXF-U4_c
              author: Ahmed
              message: Hello World
          -Jdo71zU5qsL5rcvBzRl
              author: Osama
              message: Hi!
  </code></pre>
  
  <p><br>
  Thank you.</p>