【问题标题】:Sort listview with array adapter使用数组适配器对列表视图进行排序
【发布时间】:2012-04-11 23:36:14
【问题描述】:

我有一个连接到自定义数组适配器的列表视图。此列表显示 TCP 连接接收到的更改数据集的信息...

我可以使用sort (Comparator<? super T> comparator) 对列表视图进行排序,但是当数据集更改时,列表视图不再排序...

我可以在每次更改数据集时使用sort (),但我认为这不是最好的选择...

我该怎么做?有什么建议吗?

编辑

我在实施所提供的解决方案时遇到问题...

MyComparatorB.java

public class MyComparatorB implements Comparator<DeviceB> {

private byte orderType;

public MyComparatorB(byte type) {

    this.orderType = type;

}

public int compare(DeviceB lhs, DeviceB rhs) {

    int res = 0;
    if (orderType == SortType.ALPHA) {
            res = (lhs.getName()).compareTo(rhs.getName());
        }
        else if (orderType == SortType.LAST_ACT) {
            res = (rhs.getTime().getTime()).compareTo(lhs.getTime().getTime());
        }
        return res;
    }

}

我的 customArrayAdapter.java 的片段

    @Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}


//-----------Order the content of the arrayAdapter------------//
public void sort(byte sortType) {

    super.sort(new MyComparatorB(sortType));
    notifyDataSetChanged();
}

在我的 MyActivity.java 中

myDevAdapter.sort(SortType.ALPHA);

当我调试时,方法super.sort(new MyComparatorB(sortType));被调用,MyComparatorB的构造函数也被调用。但是方法compare(DeviceB lhs, DeviceB rhs) 从未被调用,并且我的arrayList 没有排序...... 我做错了什么?

【问题讨论】:

  • 为什么不使用ArrayList对数组排序和恢复数组列表!
  • 我认为你应该对数组进行排序并调用 adapter.notifyDataSetChanged()
  • @amp : 这里的 SortType 是什么?
  • @YuDroid:这只是一个我定义可能的排序类型的类。喜欢:public class SortType{ public final static int ALPHA= 1; public final static int LAST_ACT= 2;}

标签: android listview sorting


【解决方案1】:

我猜你需要重写适配器中的 notifyDataSetChanged 方法并在调用它的 super 之前执行排序。见下文:

@Override
public void notifyDataSetChanged() {
    //do your sorting here

    super.notifyDataSetChanged();
}

这样做会在您调用notifyDataSetChanged 方法刷新列表项时对列表进行排序。否则,将排序的列表/数组提供给您的适配器。


或者更理想的是,使用适配器中可用的sort 方法来完成工作。

adapter.sort(new Comparator<String>() {
    @Override
    public int compare(String lhs, String rhs) {
        return lhs.compareTo(rhs);   //or whatever your sorting algorithm
    }
});

【讨论】:

  • 唯一的方法是每次dataSet改变时调用super.sort(comparator)方法?
  • 是的,在调用super.notifyDataSetChanged之前添加这个,这样你的数据每次都排序
  • .sort 函数会自动调用 notifyDataSetChanged,因此如果您将此代码与内置排序一起使用,您将进入无限循环并溢出堆栈。仅当您自己管理列表时才这样做,但大多数使用阵列适配器的人都不会这样做。
【解决方案2】:
  • 将数据存储到 ArrayList
  • 将阵列连接到适配器
  • 对数组进行排序并赋值给自己
  • 使用 notifyDataSetChanged() 刷新 Adapter

【讨论】:

    【解决方案3】:

    这对我有用

    @Override
    public void notifyDataSetChanged() {
        this.setNotifyOnChange(false);
    
        this.sort(new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lhs.compareTo(rhs);
            }
        });
    
        this.setNotifyOnChange(true);
    }
    

    【讨论】:

      【解决方案4】:

      根据 elBradford 关于 notifyDataSetChanged() 方法可能导致堆栈溢出的评论,我最终重写了 ArrayAdapter 的 add() 方法,以确保项目在逐个添加时保持排序的一致性。 (在调用 addAll() 时,我正在对之前的项目进行排序;但我想您也可以覆盖 addAll())。

          @Override
          public void add(ListThing object) {
              super.add(object);
              this.sort(ListThing.sComparator);
          }
      

      【讨论】:

        【解决方案5】:

        既然是Adapter填充ListView,那么唯一的办法就是将数据对象以排序的方式传递。

        【讨论】:

          【解决方案6】:

          listView 排序后,从您的活动中调用 adapter.notifyDataSetChanged()。

          【讨论】:

            【解决方案7】:

            基于waqaslamKristianR 的回答:

            @Override
            public void notifyDataSetChanged() {
                this.setNotifyOnChange(false);
            
                this.sort(new Comparator<String>() {
            
                    @Override
                    public int compare(String s1, String s2) {
                        return s1.compareTo(s2);
                    }
                });
            
                super.notifyDataSetChanged();
                this.setNotifyOnChange(true);
            }
            

            希望对你有帮助。

            【讨论】:

              【解决方案8】:

              初始化 ArrayAdapter 并声明我们正在将字符串转换为视图

              val arrayAdapter = ArrayAdapter<String>(
                          requireActivity(),
                          R.layout.item_sample,
                          R.id.tv_item_sample,
                          samplesArray
              )
              

              使用 setAdapter() 将 Adapter 设置为 ListView

              binding.lvSample.adapter = arrayAdapter
              

              执行排序并更新适配器。

              arrayAdapter.sort { lhs, rhs -> lhs!!.compareTo(rhs!!) }
              arrayAdapter.setNotifyOnChange(true)
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-02-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多