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 an item at the beginning of my list in order to display it in reverse order?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- how to split a list into a given number of sub-lis
- Listening to outgoing sms not working android
mBlogList is a recycler view...
You could always have a datestamp in your objects and sort your listview based on that..
now sort your list
This should sort your list such that the newest item will go on top
The ListView displays the data as it is stored in your data source.
When you are adding in your database, it must be adding the elements in the end. So, when you are getting all the data via the Cursor object and assigning it to the ArrayAdapter, it is in that order only. You should basically be trying to put data in the beginning of the database, rather that in the end, by having some time-stamp maybe.
Using ArrayList, you can do it by
Collections.reverse(arrayList)
or if you are using SQLite, you can useorder by
.You can add element at the beginning of the list: like
then it will always display the new element at the top.
You should probably use an
ArrayAdapter
and use theinsert(T, int)
method.Ex:
You can always use a LinkedList instead and then use addFirst() method to add elements to your list and it will have the desired behaviour (new items at the top of the ListView).