在回收站视图中按大小,日期,名称等排序并记住选择 [英] sorting in recycler view by size,date,name .etc and remember choice

查看:1757
本文介绍了在回收站视图中按大小,日期,名称等排序并记住选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作图库应用程序,我想添加排序功能,我可以在运行时使用Comparator对项目进行排序,但是问题是,每当我退出应用程序时,列表再次从数据库中移出,并且所有列表都未排序,我想在我的应用程序中提供按日期,大小,名称等进行排序的选项,你们可以帮我吗,如果他下次运行该应用程序,我该如何提醒用户的选择?我也阅读了有关排序列表的信息,请告诉我我的解决方案是什么问题或当前的任何示例代码,我正在执行此操作以对项目进行排序

I am making gallery app and I want to add sorting to it I can sort item at run time by using Comparatorbut problem is whenever I quit app the list came form database again and all the list is unsorted I want to give option in my app to sort by date,size, name etc can you guys help how can i ahcevie this and remmeber user's choice if he next time run the app I have also read about sorted-list please tell me what is solution to my problem or any sample code currently I am doing this like this to sort items

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来实现吗?或任何示例代码

this is working fine but i want to remember user sorting choice please someone help what should i do? can i achieve this by using SortedList? or any sample code please

推荐答案

这可帮助您根据名称和日期对列表进行排序.

This help you to sort list, based on Name and Date.

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;
    }

这篇关于在回收站视图中按大小,日期,名称等排序并记住选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆