Android 在 ListView 顶部显示新项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12221333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display new items at the top of a ListView
提问by rphello101
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?
我正在使用列表来填充 ListView ()。用户可以将项目添加到列表中。但是,我需要将项目显示在 ListView 的顶部。如何在列表的开头插入一个项目以便以相反的顺序显示它?
采纳答案by aymeric
You should probably use an ArrayAdapter
and use the insert(T, int)
method.
您可能应该使用 anArrayAdapter
并使用该insert(T, int)
方法。
Ex:
前任:
ListView lv = new ListView(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.id...);
lv.setAdapter(adapter);
...
adapter.insert("Hello", 0);
回答by kosa
By default list adds elements at bottom. That is why all new elements you add will show at bottom. If you want it in reverse order, may be before setting to listadapter/view reverse the list
默认列表在底部添加元素。这就是为什么您添加的所有新元素都会显示在底部的原因。如果你想要它以相反的顺序,可能在设置为 listadapter/view 之前反转列表
Something like:
就像是:
Collections.reverse(yourList);
回答by mes
Another solution without modifying the original list, override getItem() method in the Adapter
另一种不修改原始列表的解决方案,覆盖Adapter中的getItem()方法
@Override
public Item getItem(int position) {
return super.getItem(getCount() - position - 1);
}
Updated: Example
更新:示例
public class ChatAdapter extends ArrayAdapter<ChatItem> {
public ChatAdapter(Context context, List<ChatItem> chats) {
super(context, R.layout.row_chat, chats);
}
@Override
public Item getItem(int position) {
return super.getItem(getCount() - position - 1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.row_chat, parent, false);
}
ChatItem chatItem = getItem(position);
//Other code here
return convertView;
}
}
}
回答by Swayam
The ListView displays the data as it is stored in your data source.
ListView 显示存储在数据源中的数据。
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.
当您在数据库中添加时,它必须在最后添加元素。因此,当您通过 Cursor 对象获取所有数据并将其分配给 ArrayAdapter 时,仅按该顺序进行。您基本上应该尝试将数据放在数据库的开头,而不是最后,通过一些时间戳。
Using ArrayList, you can do it by Collections.reverse(arrayList)
or if you are using SQLite, you can use order by
.
使用 ArrayList,您可以通过Collections.reverse(arrayList)
或者如果您正在使用 SQLite,您可以使用order by
.
回答by sinay
You can add element at the beginning of the list: like
您可以在列表的开头添加元素:如
arraylist.add(0, object)
then it will always display the new element at the top.
那么它将始终在顶部显示新元素。
回答by Prodipta Mondal
mBlogList is a recycler view...
mBlogList 是一个回收器视图...
mBlogList=(RecyclerView) findViewById(R.id.your xml file);
mBlogList.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mBlogList.setLayoutManager(mLayoutManager);//VERTICAL FORMAT
回答by krilovich
You could always have a datestamp in your objects and sort your listview based on that..
你总是可以在你的对象中有一个日期戳,并根据它对你的列表视图进行排序。
public class CustomComparator implements Comparator<YourObjectName> {
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
这应该对您的列表进行排序,以便最新的项目将排在最前面
回答by Pedro Trindade
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).
您始终可以使用 LinkedList,然后使用 addFirst() 方法将元素添加到您的列表中,它将具有所需的行为(ListView 顶部的新项目)。