一项活动,多个视图?
我想设计一个列表,其中包含按升序或降序排序的选项。我能够使用不同的意图来做到这一点。有没有办法在没有新意图的情况下做到这一点? 我想到的例子是 Android 中的管理应用程序。按名称和大小排序发生在同一屏幕上。这是怎么做到的?
编辑 - 我有一个包含 20 个项目的列表。现在,我正在对列表项进行排序并仅显示前 5 个项目。我想添加一个选项来显示底部 5 项。我通过创建一个与上一个类完全相同的新类来完成此操作,其中顶部数组被底部数组替换。
I want to design a list with options to sort it in ascending or descending orders. I am able to do that using different intents. Is there a way to do that without having a new intent?
The example that comes to my mind is Manage Applications in android. Sort by name and size happen on the same screen. How is that done?
Edit - I have a list of say 20 items. Right now, I am sorting the list items and displaying only the top 5 items. I want to add an option to display the bottom 5 items too. I have done that by creating a new class exactle the same as prev class with top array replaced by bottom array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在将底层数据结构设置到适配器中之前对其进行重新排序。
You can re-sort the underlying data structure before setting it in the adapter.
通常使用
ListView
显示列表。ListViews
通常有一个与之关联的适配器。视图按照它们在适配器中存在的顺序列出。因此,要更改列表中元素的顺序,您所要做的就是为其设置一个新的适配器,其中元素按照您的意愿排序(您也可以尝试更改现有适配器的元素顺序,但是不知道能不能实现)。您可以查看ListView Hello World示例来更好地理解
ListViews
。Usually a list is displayed using a
ListView
.ListViews
usually have an adapter associated with them. The Views are listed in the order they exist in the adapter. So to change the order of elements in the list , all you have to do is set a new adapter to it where the the elements are ordered as you wish (You could also try to change of the order of elements of the existing adapter, but I don't know if it can be done).You can see the ListView Hello World Example to have a better understanding of
ListViews
.Umang,我现在手头没有eclipse,我只是想表达一下我解决你的问题的思路。请自行编写代码并测试。
private int mode;
模式
,根据需要返回项目。我认为您已经完成了上述步骤,因为您已经可以更改订单了。
public boolean onCreateOptionsMenu (Menu menu)
。public boolean onOptionsItemSelected (MenuItem item)
中,您可以根据用户选择的项目索引设置mode
值。adapter.notifyDataSetChanged()
通知系统listView的数据已更改。Umang, I don't have eclipse on hand now and I just want to express my train of thought to solve your problem. Please write the codes yourself and test it.
private int mode;
mode
, return the items as you want.I think you have finished the steps above, since you can change the order already.
public boolean onCreateOptionsMenu (Menu menu)
.public boolean onOptionsItemSelected (MenuItem item)
, you set themode
value based on the item index the user selected.adapter.notifyDataSetChanged()
to notify the system that the data for the listView has been changed.listView.invalidate()
to show the new ListView.ArrayAdapter 类有一个内置的通过“sort(Comparator 比较器)”进行排序的方法,前提是您愿意不厌其烦地编写您需要进行排序的 Comparator。我使用此方法“就地”排序,而无需使用外部重新排序的列表重新加载适配器。
查看 http://developer.android.com/reference/android/widget/ArrayAdapter .html
和 http://developer.android.com/reference/ java/util/Comparator.html
The ArrayAdapter class has a built-in method for sorting via "sort (Comparator comparator)", provided you are willing to go to the trouble to write the Comparators that you need to do the sorting. I use this method to sort "in place", without having to re-load the adapter with an externally re-ordered list.
Check out http://developer.android.com/reference/android/widget/ArrayAdapter.html
and http://developer.android.com/reference/java/util/Comparator.html