Display new items at the top of a ListView

前端 未结 8 861
北恋
北恋 2021-01-02 06:28

I\'m using a list to populate a ListView (). The user is able to add items to the list. However, I need the items to be displayed at the top of the ListView. How do I insert

8条回答
  •  长情又很酷
    2021-01-02 06:38

    You could always have a datestamp in your objects and sort your listview based on that..

       public class CustomComparator implements Comparator {
            public int compare(YourObjectName o1, YourObjectName o2) {
                return o1.getDate() > o2.getDate() // something like that.. google how to do a compare method on two dates
            }
        }
    

    now sort your list

    Collections.sort(YourList, new CustomComparator()); 
    

    This should sort your list such that the newest item will go on top

提交回复
热议问题