按大小、日期、名称等在回收站视图中排序并记住选择

sorting in recycler view by size,date,name .etc and remember choice

我正在制作图库应用程序,我想向其添加排序我可以在 运行 时间使用 Comparator 对项目进行排序,但问题是每当我退出应用程序时,列表再次来自数据库并且所有列表都未排序 我想在我的应用程序中提供按日期、大小、名称等排序的选项 你们能帮忙吗?如果他下次 运行 我也读过的应用程序,我怎样才能 ahcevie 并记住用户的选择关于排序列表,请告诉我什么是我的问题的解决方案或目前我正在这样做的任何示例代码来对项目进行排序

public static final Comparator<MediaFileObject> byName=new Comparator<MediaFileObject>() {
    @Override
    public int compare(MediaFileObject o1, MediaFileObject o2) {

        return o1.getAddedDate().compareTo(o2.getAddedDate());
    }
};

我在我的片段中称这个

Collections.sort(list,MediaFileObject.byName);
galleryAdapter.notifyDataSetChanged()

这工作正常,但我想记住用户排序选择请有人帮助我该怎么办? 我可以使用 SortedList 来实现吗?或任何示例代码

您可以创建一个选项列表,例如按日期、大小、名称等排序。将其中一个选项设置为默认选项并保存到 sharedpreference。当用户更改排序类型时,将所选选项设为默认选项。

当您从数据库中读取数据时,根据选择的排序选项,应用集合排序。 我相信不需要代码片段。

这可以帮助您根据姓名和日期对列表进行排序。

if (theArrayList.size() > 0) {
            if (myUserSelectedSortedType == SORT_BY_NAME) {
                sortListByName(theArrayList);
            } else if (myUserSelectedSortedType == SORT_BY__DATE) {
                sortListByDate(theArrayList);
            }


            myAdapter = new Adapter(MainActivity.this, theArrayList);
            myListView.setAdapter(myAdapter);
        }


    private void sortListByName(ArrayList<CustomerEvents> theArrayListEvents) {

          Collections.sort(theArrayListEvents, new EventDetailSortByName());
    }


     private class EventDetailSortByName implements java.util.Comparator<CustomerEvents> {
        @Override
        public int compare(CustomerEvents customerEvents1, CustomerEvents customerEvents2) {
            String name1, name2;
            name1 = customerEvents1.getMyCustomerName().toLowerCase().trim();
            name2 = customerEvents2.getMyCustomerName().toLowerCase().trim();
            return name1.compareTo(name2);
        }
    }


     private void sortListByDate(ArrayList<CustomerEvents> theArrayListEvents) {
            Collections.sort(theArrayListEvents, new EventDetailSortByDate()); 
    }



    private class EventDetailSortByDate implements java.util.Comparator<CustomerEvents> {
        @Override
        public int compare(CustomerEvents customerEvents1, CustomerEvents customerEvents2) {
            Date DateObject1 = StringToDate(customerEvents1.getMyDOB());
            Date DateObject2 = StringToDate(customerEvents2.getMyDOB());

            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(DateObject1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(DateObject2);

            int month1 = cal1.get(Calendar.MONTH);
            int month2 = cal2.get(Calendar.MONTH);

            if (month1 < month2)
                return -1;
            else if (month1 == month2)
                return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);

            else return 1;
        }
    }


     public static Date StringToDate(String theDateString) {
        Date returnDate = new Date();
        if (theDateString.contains("-")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
            try {
                returnDate = dateFormat.parse(theDateString);
            } catch (ParseException e) {
                SimpleDateFormat altdateFormat = new SimpleDateFormat("dd-MM-yyyy");
                try {
                    returnDate = altdateFormat.parse(theDateString);
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
            }
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            try {
                returnDate = dateFormat.parse(theDateString);
            } catch (ParseException e) {
                SimpleDateFormat altdateFormat = new SimpleDateFormat("dd/MM/yyyy");
                try {
                    returnDate = altdateFormat.parse(theDateString);
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return returnDate;
    }