【问题标题】:Sort array adapter after item add添加项目后对数组适配器进行排序
【发布时间】:2013-02-17 01:34:34
【问题描述】:

如何强制数组适配器自行排序。 notifyDataSetChanged 不起作用。我想要做的是将一个项目添加到数组 adpater,然后强制它再次排序。 这是我的适配器:

    this.noteListViewAdapter = new NoteListViewAdapter(this.GetActivityContext(), R.layout.list_view_note_item, noteItems);

    // sorting
    this.noteListViewAdapter.sort(new Comparator<NoteItem>()
    {
        @Override
        public int compare(NoteItem item1, NoteItem item2)
        {
            // TODO Auto-generated method stub
            return item2.CreationDate.compareTo(item1.CreationDate);
        };
    });

它工作正常。项目已排序。但是当我想添加新项目时,排序不起作用。它甚至没有被调用。 这就是我添加新项目的方式:

    this.noteListViewAdapter.add(note);
    this.noteListViewAdapter.notifyDataSetChanged();

感谢您的帮助

【问题讨论】:

  • 在 add() 之后在 notifyDataSetChanged() 之前再次排序;

标签: android sorting adapter


【解决方案1】:

在将传递的note 添加到noteItems 的内部列表之后,您需要在add() 方法中调用sort()

或者你可以在调用super.notifyDataSetChanged()之前在notifyDataSetChanged()内部调用它。

我认为后者更好,因为这样你就不会每次都在批处理-add()ing 项目时进行排序。

【讨论】:

    【解决方案2】:

    不要在 notifyDataSetChanged() 中调用 sort() ,否则你会得到一个 StackOverFlow ! :)

    sort() 已经调用 notifyDataSetChanged()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 2018-04-30
      • 2014-02-07
      • 1970-01-01
      • 2012-04-11
      • 2010-10-11
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多