user45678
user45678

Reputation: 1514

How to add new items in listview at the top in android

I am having a ListView in which i am showing list items fetched from shared preference, now when every time new list item includes it includes below the first one, is there any way to update listview in such a way that new item included will be at the top. In short incoming item should be at top. Below is my code base :

I tried

Collections.reverse(yourList);

before setting adapter but it works sometimes and sometimes when again i open activity it won't work

Upvotes: 1

Views: 3278

Answers (2)

lithos35
lithos35

Reputation: 250

Can you try by replacing the

items.add(new EntryItem(first, second));

in addItems() by

items.add(0, new EntryItem(first, second));

Upvotes: 3

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

You can use the following attribute in your XML in your ListView node, however I might be wrong, but I actually think ListView stacks on top already by default...

android:stackFromBottom

Used by ListView and GridView to stack their content from the bottom.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol stackFromBottom.

http://developer.android.com/reference/android/widget/AbsListView.html#attr_android%3astackFromBottom

Upvotes: 0

Related Questions