How to use sorting and Filter List data in Android

Hi everyone in this article, I am sharing how to use sorting and filter list data in Android. How to filter in your recycler view list. Sorting and filter like Date Sorting, Date Range filter,  A to Z Sort and etc.

In this Android sorting and filter Android blog you can follow these simple methods and get results. For local data filter and Sort and android compare two values for soring and filter without any library.

 

Let’s Start on Android Solution to compare two values for Android sorting and filtering list data local.

in these, all Method I have used these array list And Adapter class for data added for API.

ArrayList<TicketsListData> ticketsList = new ArrayList<>();
TicketsListAdapter ticketsListAdapter;

1. Sorting on Date like filters data on Older Date. 

Collections.sort(ticketsList, new Comparator<TicketsListData>() {
    @Override
    public int compare(TicketsListData item1, TicketsListData item2) {
        return item1.getTicketCreateTime().compareToIgnoreCase(item2.getTicketCreateTime());

    }
});
ticketsListAdapter.notifyDataSetChanged();

Used this method for older date sorting a list in Android hare ticketsList  is an array list of data, And TicketsListData is a getter and setter class to get and set value. getTicketCreateTime is an object on Data Class getter and setter to compare the value.

2. How to Reverse A sort latest item list in Android.

Collections.sort(ticketsList, new Comparator<TicketsListData>() {
    @Override
    public int compare(TicketsListData item1, TicketsListData item2) {
        return item1.getTicketCreateTime().compareToIgnoreCase(item2.getTicketCreateTime());

    }
});
Collections.reverse(ticketsList);
ticketsListAdapter.notifyDataSetChanged();

3.  Android ToDate and From Date Filter in Android 

public void FilterData(){
    String Start = SortDate1.getText().toString().trim();
    String EndDate =SortDate2.getText().toString().trim();


    if(!Start.equals("")||!EndDate.equals("")||!Start.equals(null)||!EndDate.equals(null)){
        try {
            dateFormatter=new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date strDate = dateFormatter.parse(Start);
            Date endDate = dateFormatter.parse(EndDate);
            ticketsListAdapter.filterDateRange(strDate,endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }else {

    }
}

Android Adapter Class.

public class TicketsListAdapter extends RecyclerView.Adapter<TicketsListAdapter.TicketsListViewHolder>{
    public ArrayList<TicketsListData> ticketList;
    public ArrayList<TicketsListData> filteredTicketList;
    public Activity mActivity;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate;


    public TicketsListAdapter(ArrayList<TicketsListData> list, Activity activity) {
        ticketList = list;
        this.filteredTicketList = new ArrayList<TicketsListData>();
        this.filteredTicketList.addAll(list);
        mActivity = activity;
    }

    @Override
    public TicketsListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ticket_list, parent, false);

        TicketsListViewHolder holder = new TicketsListViewHolder(view, mActivity, ticketList);

        return holder;
    }

    @Override
    public void onBindViewHolder(final TicketsListViewHolder holder, int position) {
        holder.tvTicketNumber.setText((ticketList.get(position).getTicketNumber()));
//        holder.tvTicketLoggedTime.setText((ticketList.get(position).getTicketLoggedTime()));
        holder.tvTicketHeader.setText((Html.fromHtml(ticketList.get(position).getTicketHeading())));
        String severity = ticketList.get(position).getImpact();
        holder.ticketSeverity.setText("S"+ticketList.get(position).getImpact());
        if (severity.equalsIgnoreCase("1")){
            holder.ticketSeverity.setTextColor(mActivity.getResources().getColor(R.color.light_red));
        }else  if (severity.equalsIgnoreCase("2")){
            holder.ticketSeverity.setTextColor(mActivity.getResources().getColor(R.color.light_yellow));
        }else  if (severity.equalsIgnoreCase("3")){
            holder.ticketSeverity.setTextColor(mActivity.getResources().getColor(R.color.Bluelight));
        }else{
            holder.ticketSeverity.setTextColor(mActivity.getResources().getColor(R.color.Green2));
        }

        try {
            Date d = dateFormat.parse(ticketList.get(position).getTicketLoggedTime());
            Format formatter = new SimpleDateFormat("EE");
            DateFormat date = new SimpleDateFormat("MMM");
            DateFormat Day = new SimpleDateFormat("dd");
            DateFormat Year = new SimpleDateFormat("yyyy");
            String Date = date.format(d);
            String DayName = formatter.format(d);
            String day =Day.format(d);
            String Years =Year.format(d);
            formattedDate = DayName+", "+Date+" "+day+"th, "+Years;
            holder.tvTicketLoggedTime.setText(formattedDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return ticketList.size();
    }


    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        ticketList.clear();
        if (charText.length() == 0) {
            ticketList.addAll(filteredTicketList);
        } else {
            for (TicketsListData wp : filteredTicketList) {
                if (wp.getTicketNumber().toLowerCase(Locale.getDefault()).contains(charText)) {
                    ticketList.add(wp);
                }else if(wp.getImpact().toLowerCase(Locale.getDefault()).contains(charText)){
                    ticketList.add(wp);
                }else if(wp.getTicketHeading().toLowerCase(Locale.getDefault()).contains(charText)){
                    ticketList.add(wp);
                }else {

                }
            }
        }
        notifyDataSetChanged();
    }


    // Filter Class
    public void filter1(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        ticketList.clear();
        if (charText.length() == 0) {
            ticketList.addAll(filteredTicketList);
        } else {
            for (TicketsListData wp : filteredTicketList) {
               if(wp.getImpact().toLowerCase(Locale.getDefault()).contains(charText)){
                    ticketList.add(wp);
                }else {

                }
            }
        }
        notifyDataSetChanged();
    }

    // Filter Class
    public void filterDateRange(Date charText,Date charText1) {
        ticketList.clear();
        if (charText.equals("")||charText.equals(null)) {
            ticketList.addAll(filteredTicketList);
        } else {
            for (TicketsListData wp : filteredTicketList) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date strDate = sdf.parse(wp.getTicketLoggedTime());
                    if (charText1.after(strDate)&&charText.before(strDate)) {
                        ticketList.add(wp);
                    }else{

                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        }
        notifyDataSetChanged();
    }


    class TicketsListViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ticketNumberCardView)
        CardView ticketNumberCardView;
        @BindView(R.id.ticketNumber)
        TextView tvTicketNumber;
        @BindView(R.id.ticketLoggedTime)
        TextView tvTicketLoggedTime;
        @BindView(R.id.ticketHeader)
        TextView tvTicketHeader;
        @BindView(R.id.ticketSeverity)
        TextView ticketSeverity;


        public TicketsListViewHolder(View view, final Activity activity, ArrayList<TicketsListData> ticketList) {
            super(view);
            ButterKnife.bind(this, view);
            ticketNumberCardView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                TicketsListData ticketsListData = ticketList.get(position);
                Intent ticketDetailsIntent = new Intent(activity, TicketDetailsActivity.class);
                ticketDetailsIntent.putExtra("caseNumber", ticketsListData.getTicketNumber());
                ticketDetailsIntent.putExtra("DATETIME", formattedDate);
                activity.startActivity(ticketDetailsIntent);
                activity.overridePendingTransition(R.anim.left_slide, R.anim.right_slide);

            });
        }
    }


}

 

Facebooktwitterredditpinterestlinkedinmail
Recommended
Hii Developer in this Android example we have implemented  Bar…