一项活动,多个视图?

发布于 2024-12-13 16:28:35 字数 237 浏览 7 评论 0原文

我想设计一个列表,其中包含按升序或降序排序的选项。我能够使用不同的意图来做到这一点。有没有办法在没有新意图的情况下做到这一点? 我想到的例子是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烦人精 2024-12-20 16:28:35

您可以在将底层数据结构设置到适配器中之前对其进行重新排序。

You can re-sort the underlying data structure before setting it in the adapter.

庆幸我还是我 2024-12-20 16:28:35

通常使用 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.

雨的味道风的声音 2024-12-20 16:28:35

Umang,我现在手头没有eclipse,我只是想表达一下我解决你的问题的思路。请自行编写代码并测试。

  1. 必须有一个与你的listview关联的适配器,在它内部,你可以设置一个变量来监控订单模式,如: private int mode;
  2. 在适配器的 getView() 方法内部,根据模式,根据需要返回项目。
    我认为您已经完成了上述步骤,因为您已经可以更改订单了。
  3. 在第一个活动中,您可以为用户设置一些事件来更改顺序,即通过菜单。因此,您重写public boolean onCreateOptionsMenu (Menu menu)
  4. 在相应的 public boolean onOptionsItemSelected (MenuItem item) 中,您可以根据用户选择的项目索引设置 mode 值。
  5. 调用adapter.notifyDataSetChanged()通知系统listView的数据已更改。
  6. 调用listView.invalidate()来显示新的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.

  1. There must be an adapter associated with your listview, inside it, you can set a variable to moniter the order mode, like: private int mode;
  2. Inside the getView() method of the adapter, based on the mode, return the items as you want.
    I think you have finished the steps above, since you can change the order already.
  3. In the first Activity, you can set some event for the user to chage the order, i.e, via the Menu. So you override public boolean onCreateOptionsMenu (Menu menu).
  4. In the corresponding public boolean onOptionsItemSelected (MenuItem item), you set the mode value based on the item index the user selected.
  5. Call adapter.notifyDataSetChanged() to notify the system that the data for the listView has been changed.
  6. call listView.invalidate() to show the new ListView.
指尖微凉心微凉 2024-12-20 16:28:35

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文