【问题标题】:arrange cutom Arraylist according to time column根据时间列排列自定义Arraylist
【发布时间】:2018-07-29 14:42:01
【问题描述】:

我有一个自定义数组列表,其中包含多个参数以及时间。

我想按照升序和降序格式的时间参数对该列表进行排序

我搜索了很多 这里有一些链接 Sort objects in ArrayList by date?

Sort ArrayList with times in Java

还有更多 但没有一个工作

这是我的代码

点击函数调用

 private void callTimeSorting(ArrayList<ObjectClass>solutions){
    try{
        ArrayList<ObjectClass> tempList = new ArrayList<>();
        if (tempList.size() > 0){tempList.clear();}
        tempList.addAll(solutions);
        if (mTimeTag == 1) {
            if (!mAsendingTime) {
                Collections.sort(tempList, AsendingTimeComparator);
                mAsendingTime = true;
                mAdapter.notify();
            } else {
                Collections.sort(tempList, DesendingTimeComparator);
                mAsendingTime = false;
            }

        }
 }

OnClick 方法

 @Override
public void onClick(View view) {
    int id = view.getId();
    switch (id) {
       case R.id.tvTimings :
            mTimeTag =  1 ;
            callTimeSorting(mSolutions) ;
            mAdapter.notifyDataSetChanged();
            break;


 }}

AsendingComparator

  private static Comparator<ObjectClass> AsendingTimeComparator = new Comparator<ObjectClass>() {
    @Override
    public int compare(ObjectClass solution1, ObjectClass solution2) {
        String time1 = solution1.getDepartureDateTime();
        String time2 = solution2.getDepartureDateTime();
        return time1.compareTo(time2);//asending
    }
};

private static Comparator<ObjectClass> DesendingTimeComparator = new Comparator<ObjectClass>() {
    @Override
    public int compare(ObjectClass solution1, ObjectClass solution2) {
        String time1 = solution1.getDepartureDateTime();
        String time2 = solution2.getDepartureDateTime();
        return time2.compareTo(time1);//descending

    }



};

我不知道为什么我的排序列表没有附加到 recyclerView??

【问题讨论】:

  • 在设置适配器中您使用的是哪个列表?

标签: android


【解决方案1】:

使用 mList 的引用 ...在整个代码中不要再次初始化它。使用 clear() 而不是 new Arraylist&lt;&gt; 并且不要为列表分配任何其他引用,例如 @987654323 @

mAdapter 将失去对列表的引用...并且不会相应地工作..(notifyDataSetChanged() 不会工作。

【讨论】:

    【解决方案2】:

    您的列表未排序,因为您正在对 tempList 进行排序:

    Collections.sort(tempList, AsendingTimeComparator);
    

    而不是您的适配器正在使用的实际列表,即解决方案。

    您不应该使用 templist 并直接排序解决方案。

    private void callTimeSorting(ArrayList<ObjectClass>solutions){
        try{
            if (mTimeTag == 1) {
                if (!mAsendingTime) {
                    Collections.sort(solutions, AsendingTimeComparator);
                    mAsendingTime = true;
                    mAdapter.notify();
                } else {
                    Collections.sort(solutions, DesendingTimeComparator);
                    mAsendingTime = false;
                }
    
            }
     }
    

    【讨论】:

      【解决方案3】:

      使用适配器中使用的原始列表,而不是临时列表

       private void callTimeSorting(ArrayList<ObjectClass>solutions){
              try{
                  ArrayList<ObjectClass> tempList = new ArrayList<>();
                  tempList.addAll(solutions);
                  solutions.clear();
                  if (mTimeTag == 1) {
                      if (!mAsendingTime) {
                          Collections.sort(tempList, AsendingTimeComparator);
                          mAsendingTime = true;
                      } else {
                          Collections.sort(tempList, DesendingTimeComparator);
                          mAsendingTime = false;
                      }
                    solutions.addAll(tempList);
                    mAdapter.notifyDataSetChanged();
                  }
           }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多