25

ArrayAdapter has method add(T object) which add an object at the end of a list. Is there a way to add object at the beginning of the list?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
krinn
  • 882
  • 2
  • 10
  • 16

1 Answers1

55

You can use the .insert(T object, int index) method for this, using 0 as an index:

yourAdapter.insert(object, 0);
Cat
  • 66,919
  • 24
  • 133
  • 141
  • it's work but when I use this method it's also scroll my listview to top. For example my listview is at position 0, I add 5 elements before (at position 0) and my listview scrolls at position 0, but it shoud stay at last selected position (6) – krinn Oct 23 '12 at 17:40
  • You need to do that manually. It re-inflates the list from scratch, so this will be an expected side-effect. – Cat Oct 23 '12 at 17:44
  • @krinn You may such a workaround http://stackoverflow.com/questions/15597450/android-listview-add-items-to-top-without-list-view-scroll to retain the scroll position – riwnodennyk Jul 21 '14 at 14:14
  • can we have anything for List class object? As currently we add in apter like this `adapter.addAll(messageList);` so there is any way to set list at the top of the listview? – Faisal Shaikh Mar 07 '16 at 16:14