[英]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. 我有一个ListView,其中我显示从共享首选项中获取的列表项,现在当每次新列表项包括它包括在第一个下面时,是否有任何方式以包含新项目的方式更新listview顶端。 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 在设置适配器之前,它有时会工作,有时当我再次打开活动时,它将无法工作
Can you try by replacing the 你可以尝试更换
items.add(new EntryItem(first, second));
in addItems() by 在addItems()中
items.add(0, new EntryItem(first, second));
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... 您可以在ListView节点中的XML中使用以下属性,但是我可能错了,但我实际上认为ListView在默认情况下已经在顶部堆叠...
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 http://developer.android.com/reference/android/widget/AbsListView.html#attr_android%3astackFromBottom
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.