tccpg288 tccpg288 - 7 months ago 91
Android Question

Invert ListView Order and Display Without Scrolling

I am trying to invert a ListView so that the most recent items appear first. I have seen results that state to modify the getItem() method, however that requires me scrolling down and scrolling back up to see the new item. Is there a way to have the item appear at the top of the list without requiring the need to scroll?

public class ListAdapter extends ArrayAdapter<Comments> {

Firebase BaseRef = new Firebase(FIREBASE_URL);
Firebase PollsRef = mBaseRef.child(POLLS_LABEL);
Firebase UpdateRef = mPollsRef.child(mCurrentDateString).child(String.valueOf(mPollIndex + 1));
Firebase CommentsRef = mUpdateRef.child(COMMENTS_LABEL);
int pollCommentCount;

public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);

public ListAdapter(Context context, int resource, List<Comments> items) {
super(context, resource, items);

public int getCount() {
CommentsRef.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
pollCommentCount = (int) dataSnapshot.getChildrenCount();
Log.v("POLL_COMMENT_COUNT", "The poll comment count is " + pollCommentCount);

public void onCancelled(FirebaseError firebaseError) {

return pollCommentCount;

public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.individual_comment, null);

Comments p = getItem(position);

if (p != null) {
TextView userID = (TextView) v.findViewById(;
TextView userComment = (TextView) v.findViewById(;

if (userID != null) {

if (userComment != null) {

return v;



You can sort the Comment list before creating your adapter. This way they are already in the order you want them to be in. I don't know what variable the Comment object contains that lets you know when it was modified, but assuming it is a date, you can sort the list like this:

Collections.sort(commentsList, new Comparator<Comment>() {
    public int compare(Comment c1, Comment c2) {
        return c1.getDate().compareTo(c2.getDate());

You can also simply reverse the list with Collections.reverse(commentList)