我创建了一个 ListActivity
(是的,没有 XML 布局的)显示从 ArrayList<String>
获取的值,通过adapter
.
事实是最后一个字符串添加到 ArrayList
,是最新出现在 ListView
上的。另外,我想对 ListView
进行排序这样添加的最后项目将是ListView
中顶部上的第一个项目。 ,怎么办?
看,这是我的应用程序的一个 fragment ,关于 onCreate()
ListActivity
的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
ListView historyList = getListView();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, history);
historyList.setAdapter(listAdapter);
}
最佳答案
没什么复杂的。您只需要反转列表历史记录即可。
Collections.reverse(history);
您应该阅读一些有关 Java 的理论并了解如何查找。不然什么好事都没有结束。代码如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
Collections.reverse(history);
ListView historyList = getListView();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, history);
historyList.setAdapter(listAdapter);
}
关于java - 对 ListActivity 中的 ListView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679818/