Android如何按列对列表视图中的对象列表进行排序

发布于 2024-12-05 14:22:13 字数 134 浏览 0 评论 0原文

我在android列表视图中使用适配器绑定类对象列表,列表视图有3个列标题(3个标题按钮),每个标题都有单击事件,现在我想按列对列表视图进行排序意味着当我单击第一个标题列时,数据相对于第一列排序,我单击第二个标题相对于第二列排序数据。我该如何做到这一点。

I bind list of class objects using adapters in android list view, the list view have 3 column headers(3 header buttons), each header have click event, now i want to sort list view by column means when ever i click on first header column, the data sorted with respect to first column, i click second header sorted the data with respect to second column.How can i do this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空气里的味道 2024-12-12 14:22:13

前面有大量伪代码,我们已警告您。

假设您有一个 Foo 类,

class Foo{

private int param1;
private float param2;
private String param3;
}

现在创建 3 个比较器,一个用于您要排序的每个成员。最好使其成为同一类的静态成员。

class Foo
{
  public static Comparator PARAM1_COMPARATOR = <defination>;
  public static Comparator PARAM2_COMPARATOR = <defination>;
  public static Comparator PARAM3_COMPARATOR = <defination>;
}

在您的活动中,具有此函数 refreshList() (或类似的函数),当要更改排序顺序时会调用该函数。

void refreshList()
{
  List<Foo> list = //say this is your list

  //Pro tip: User a switch case instead.

  if(Sort_by_param1)
   Collections.sort(list, Foo.PARAM1_COMPARATOR);
  if(Sort_by_param2)
   Collections.sort(list, Foo.PARAM2_COMPARATOR);
  if(Sort_by_param3)
   Collections.sort(list, Foo.PARAM3_COMPARATOR);

  adapter.notifyDatasetChanged() or call setAdapter again with the new list.
}

Loads of pseudo code ahead, you've been warned.

Say you have a class Foo

class Foo{

private int param1;
private float param2;
private String param3;
}

Now make 3 Comparators, one for each member you want to sort with. Preferably make it a static member of the same class.

class Foo
{
  public static Comparator PARAM1_COMPARATOR = <defination>;
  public static Comparator PARAM2_COMPARATOR = <defination>;
  public static Comparator PARAM3_COMPARATOR = <defination>;
}

In your activity, have this function refreshList() (or something of that sort ) which is called when the sorting order is to be changed.

void refreshList()
{
  List<Foo> list = //say this is your list

  //Pro tip: User a switch case instead.

  if(Sort_by_param1)
   Collections.sort(list, Foo.PARAM1_COMPARATOR);
  if(Sort_by_param2)
   Collections.sort(list, Foo.PARAM2_COMPARATOR);
  if(Sort_by_param3)
   Collections.sort(list, Foo.PARAM3_COMPARATOR);

  adapter.notifyDatasetChanged() or call setAdapter again with the new list.
}
别在捏我脸啦 2024-12-12 14:22:13

按照此代码对任何 ArrayList 进行排序

  Collections.sort(empList, new Comparator<Employee>(){
  public int compare(Employee emp1, Employee emp2) {
    return emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());
  }
});

Follow this code to sort any ArrayList

  Collections.sort(empList, new Comparator<Employee>(){
  public int compare(Employee emp1, Employee emp2) {
    return emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文